Python: Manipulate string or binary bytes with StringIO

November 25th, 2008 mysurface Posted in Developer, python | Hits: 217594 | 9 Comments »

Sometimes it is not convenient to construct string using equal (=) like this:

str = "Hello, "
str = str + "my name is "
str = str + Name
print str

In python, we have string stream (StringIO) that will behave like file stream, you can construct your string like this:

str.write("Hello, ")
str.write("my name is ")
print str.getvalue()

The same way, you can construct your binary bytes with StringIO and write it into file once you are done.

bin.write("/x5F/x5F%c" % 0xFF)
file = open ("my.bin","wb")

9 Responses to “Python: Manipulate string or binary bytes with StringIO”

  1. To import the StringIO Class:

    from StringIO import StringIO

  2. Thanks André, forgot to mention how to import StringIO. :P

  3. i usually use

    str = ”
    str += ‘line 1\n’
    str += ‘line 2\n’
    str += ‘%s’ % 12345

    shorter :)

  4. Unfortunately this forum is vulnerable to cheesy FFF spambots,
    although at least it seems that the links have been filtered out. Attaboy.

  5. Thanks for pointing out, I was not aware about this previously.

  6. It’s amazing to pay a visit this web page and reading the views of all colleagues regarding this article,
    while I am also eager of getting knowledge.

  7. blogs ces uwex edu wordpress

  8. writing a response essay my essay writer do my homework for money

  9. viagra alternatives over counter viagra in budapest viagra melbourne

Leave a Reply