Python Forum
Right way to write string into UTF-8 file?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Right way to write string into UTF-8 file?
#2
Python 3 has full Unicode support and has default encoding as UTf-8.
Always for file out and in use UTF-8,
do not encode decode anything if not necessary as for taking text(Unicode as default) out and in from Python 3.
s = 'Crème and Spicy jalapeño ☂'
with open('unicode.txt', 'w', encoding='utf-8') as f_out:
    f_out.write(s)
Output on disk.
Output:
Crème and Spicy jalapeño ☂
Read in:
with open('unicode.txt', encoding='utf-8') as f:
    print(f.read())
Output:
Crème and Spicy jalapeño ☂
Reply


Messages In This Thread
RE: Right way to write string into UTF-8 file? - by snippsat - Aug-28-2018, 02:27 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020