office Can store two csv file , I call the orange box in the figure below csv by 1 Number , Red box csv by 2 Number
- Open in Notepad csv file , The lower right corner shows utf-8 Namely 1 Number , Show ANSI Namely 2 Number
Obviously, only utf-8 decode , The code is as follows
with open(file, encoding='utf-8') as f:
reader = csv.reader(f)
for row in reader:
print(row)
The attached drawings help understand :
chart 1
It can be used gbk and ansi Two kinds of encoding for decoding
in addition ,with open Our default decoder is GBK, So all three ways of writing
# How to write it 1
with open(file, encoding='gbk') as f:
reader = csv.reader(f)
for row in reader:
print(row)
# How to write it 2
with open(file) as f:
reader = csv.reader(f)
for row in reader:
print(row)
# How to write it 3
with open(file, encoding='ansi') as f:
reader = csv.reader(f)
for row in reader:
print(row)
Explain why ansi Yes, but utf-8 But not , Because the encoding format of Notepad "ANSI" Different from ASCII, It may contain gbk etc. uft-8 Incompatible characters . See :
Windows Notepad ANSI、Unicode、UTF-8 What's the difference between the three coding modes ? - You know