Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Portuguese encoding ã, ê, ç, á

I have this code:

with open('dataset.csv', "w", encoding="utf-8") as myfile:
    print(datafromAPI.decode("utf-8"),file=myfile)

which saves .csv file after decoding dataset from a data server through an API but the encoding is not displaying proper character. For instance, ã shows ã, ê shows ê, ç shows ç, á shows á, etc I have tried to change starting from encoding of notepad++ and within my python code using utf-8, latin-1, ISO 8859-1, etc with no effect.

like image 743
MGB.py Avatar asked Oct 22 '25 14:10

MGB.py


1 Answers

I'll have to look into this further, but it's possible that print is doing something weird to your strings on the way to the file.

Since you know datafromAPI is a bytes object, consider trying the three two different approaches below:

[write + text mode]

with open('dataset.csv', "w", encoding="utf-8") as myfile:
    myfile.write(datafromAPI.decode("utf-8"))

[write + binary mode]

with open('dataset.csv', "wb") as myfile:
    myfile.write(datafromAPI)

This last version will certainly work, provided you open it with an editor that supports the source encoding.

Update: removed the version I wasn't sure would even work -- it will not.

like image 188
jedwards Avatar answered Oct 24 '25 05:10

jedwards



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!