cStringIO, stringIO, and Unicode --> encode as utf-8
Answer:
cStringIO.StringIO().write(u'\u2603'.encode('utf-8'))
http://stackoverflow.com/questions/4677512/can-i-use-cstringio-the-same-as-stringio
On the other hand, if you encode the strings yourself, you can still give them to a cStringIO. For example,
cStringIO.StringIO().write(u'\u2603')
doesn't work, butcStringIO.StringIO().write(u'\u2603'.encode('utf-8'))
works fine. – rescdsk Aug 3 at 13:13
using cstringio with unicode - Google Search
http://ginstrom.com/scribbles/2008/11/16/notes-for-using-unicode-with-python-2x/
class OutStreamEncoder(object):
"""
Wraps a stream with an encoder
usage:
out = OutStreamEncoder(out, "utf-8")
"""
out = OutStreamEncoder(out, "utf-8")
"""
def __init__(self, outstream, encoding):
self.out = outstream
self.encoding = encoding
self.out = outstream
self.encoding = encoding
def write(self, obj):
"""
Wraps the output stream, encoding Unicode
strings with the specified encoding
"""
"""
Wraps the output stream, encoding Unicode
strings with the specified encoding
"""
if isinstance(obj, unicode):
self.out.write(obj.encode(self.encoding))
else:
self.out.write(obj)
self.out.write(obj.encode(self.encoding))
else:
self.out.write(obj)
def __getattr__(self, attr):
"""Delegate everything but 'write' to the stream"""
"""Delegate everything but 'write' to the stream"""
return getattr(self.out, attr)
python how to parse csv from in-memory string?
- Google Search
http://stackoverflow.com/questions/4855523/parsing-csv-data-from-memory-in-python
Answer:
There is no special distinction for files about the python csv module. You can use StringIO to wrap your strings as file-like objects.
No comments:
Post a Comment