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=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()
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 :)
November 20th, 2010 at 2:37 am
Unfortunately this forum is vulnerable to cheesy FFF spambots,
although at least it seems that the links have been filtered out. Attaboy.
November 21st, 2010 at 11:34 am
Thanks for pointing out, I was not aware about this previously.
October 19th, 2014 at 2:42 pm
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.
March 2nd, 2017 at 10:32 am
blogs ces uwex edu wordpress
November 28th, 2020 at 9:39 pm
writing a response essay my essay writer do my homework for money
December 10th, 2020 at 12:14 am
viagra alternatives over counter viagra in budapest viagra melbourne