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]](https://s22.postimg.cc/x06u6nild/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:
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:
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]](https://s22.postimg.cc/x06u6nild/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')