Python: Manipulate string or binary bytes with StringIO
November 25th, 2008 mysurface Posted in Developer, python | Hits: 149647 |
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=StringIO()
...
str.write("Hello, ")
...
str.write("my name is ")
...
str.write(Name)
print str.getvalue()
The same way, you can construct your binary bytes with StringIO and write it into file once you are done.
bin=StringIO()
bin.write("/x5F/x5F%c" % 0xFF)
...
file = open ("my.bin","wb")
file.write(bin.getvalue())
file.close()
Live Chat!







November 25th, 2008 at 11:26 pm
To import the StringIO Class:
from StringIO import StringIO
http://www.python.org/doc/2.5.2/lib/module-StringIO.html
November 26th, 2008 at 1:43 am
Thanks André, forgot to mention how to import StringIO. :P
November 28th, 2008 at 8:48 pm
i usually use
str = ”
str += ‘line 1\n’
str += ‘line 2\n’
str += ‘%s’ % 12345
shorter :)
February 2nd, 2009 at 3:34 pm
It’s ok to have a comment.
October 20th, 2009 at 3:10 pm
The best is not always the most popular. My favorite scientific blogs. Thank you admin.
November 16th, 2009 at 2:29 am
This report is misleading. Luda doesn’t claim to have hoes on Midway or Wake Islands, only in area code 808, which also includes Hawaii, a far more likely zone for hoes, it seems.
January 2nd, 2010 at 12:13 am
Thank you for adding
January 2nd, 2010 at 12:14 am
Thank you for adding
January 2nd, 2010 at 12:15 am
Thank you for adding
January 2nd, 2010 at 12:15 am
Thank you for adding
February 5th, 2010 at 10:56 am
u have a nice site i was searching about the popular site of your countr i found here. good workingz
February 5th, 2010 at 10:56 am
u have a nice site i was searching about the popular site of your countr i found here. good workings
March 17th, 2010 at 7:57 pm
tanks for you adminbey