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?
#1
Hello,

I need to append a string to a text file that's encoded in UTF-8.

It appears that, by default, Python 3 tries to write in ANSI (Latin-1, ISO8859-1, cp1252, or what ever is the correct name). As a result, I end up with a file that cannot be correctly displayed, since it uses two encoding methods in the same file.

[Image: A961_E3_BF-_F782-46_E2-839_B-129370_CDF8_B8.png]
(In ANSI, "è" is indeed 0xE8).

I tried the following but it doesn't work:

file = open("test.latin1.utf8.txt","w")
file.write("Crème")

stringutf8 ="Crème".encode('utf-8')
print(stringutf8)
#BAD Error: TypeError: write() argument must be str, not bytes

file.write(stringutf8)
file.close()
Any idea how to do this?

Thank you.

---
Edit: Python won't let me open the file in UTF8 since it detects an ANSI character ("É" = 0xc9) wrongly added by another script; And it won't let me replace that faulty string either:

#Error: UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc9 in position 327: invalid continuation byte
f = codecs.open(inputfile, "r", "utf-8")
content = f.read()
f.close()
…
filename =" Crème"
#Error: AttributeError: 'str' object has no attribute 'decode'
filename = filename.decode('utf-8')
Reply


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

Forum Jump:

User Panel Messages

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