1

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!

1 Answer 1

1

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...

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.