I have a .csv file which is encoded in UTF-8.
I am working with Python 2.7.
Something intereseting happens on Ubuntu.
When I print out the results of the file like this:
with open("file.csv", "r") as file:
myFile = csv.reader(file, delimiter = ",")
for row in myFile:
print row
I get signs like \xc3\x, \xa1\, .... Note that row is a list and all the elements in my list are marked as strings by '' in the output.
When I print out the results like this:
with open("file.csv", "r") as file:
myFile = csv.reader(file, delimiter = ",")
for row in myFile:
print ",".join(row)
Everything is decoded fine. Note that every row from my original file is one big string here.
Why is that?
print ['åäö']python will keep the internal encoding of the string as-is. Where as if you printprint ['åäö'][0]it will try to decode/encode (can never remember which it is) to whatever encoding you've defined at the top of your script (Usually 7 or 8bit ASCII). - Torxedjoin()expects strings. Strings and bytes are the same thing in Python 2.7. In Python 3 this is not the case so i am guessing you wouldn't see that there. - Ma0