Python read write file unicode
But this does not work on Python 2, right? I should said, on this Python 3 code, it looks so concise and reasonable — Liwen Zhao. We stay on Python 3. This is THE answer. This is how you properly write utf-8 to a file, thanks! KerwinSneijders the question is about Python 2. Python 2. And I don't think there should be 2 questions both for python 2 and 3, so because python 2.
Preface: will your viewer work? In Python 2, use open from the io module this is the same as the builtin open in Python 3 : import io Best practice, in general, use UTF-8 for writing to files we don't even have to worry about byte-order with utf Thomas Wouters Thomas Wouters k 22 22 gold badges silver badges bronze badges. How to print unicode characters into a file: Save this to file: foo. Eric Leschinski Eric Leschinski k 87 87 gold badges silver badges bronze badges.
I was pretty excited about this answer, but it gives an error on my machine. There are two possibilities: You're encoding it to a bytestring, but because you've used codecs. So you encode it, and it tries to decode it again. Try: f. When you do. Thomas K Thomas K Doesn't work for me getting this error: TypeError: write argument must be str, not bytes — bakalolo.
The Overflow Blog. Podcast Helping communities build their own LTE networks. Podcast Making Agile work for data science. Featured on Meta. New post summary designs on greatest hits now, everywhere else eventually. Visit chat. Linked See more linked questions. Improve this question. Add a comment. Active Oldest Votes. Improve this answer.
What about this solution? Community Bot 1 1 1 silver badge. DougieHauser DougieHauser 5 5 silver badges 14 14 bronze badges. Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. Add a comment. Active Oldest Votes.
Improve this answer. Denis Olehov Denis Olehov 2 2 silver badges 12 12 bronze badges. Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog. Podcast Helping communities build their own LTE networks. Podcast Making Agile work for data science.
0コメント