Reputation: 23516
I want to create a CSV string which is being sent by mail. I'd like to avoid temporary files because it's just not necessary for me to deal with the file system. As I said, the data is passed to a mail class.
I found this neat solution
output = io.StringIO()
writer = csv.writer(output, delimiter=';', dialect='excel-tab')
writer.writerows(data)
It works perfectly except that it creates a UTF-8 file which will have messed up special characters if you open it in Excel.
I tried to pass a BOM to the StringIO
constructor, but nothing worked:
output = io.StringIO('\ufeff')
I tried to somehow set the encoding to utf-8-sig
but I couldn't find a way except using a file...
Any ideas how to solve this problem?
Thanks!
Upvotes: 1
Views: 405
Reputation: 408
StringIO is strings which means unicode. To do what you want, I think you need to use BytesIO instead.
After reading this and this I think the problem is that StringIO is strings only and therefore no encoding.
You could try this [extrapolated from the first link above]:
bio = io.BytesIO()
StreamWriter = codecs.getwriter('utf-8-sig')
wrapper_file = StreamWriter(bio)
csv.writer(wrapper_file,...
writer.writerows(data)
Python character encoding makes my head hurt...
Upvotes: 1