读取 CSV 文件时出现奇怪的字符

Adi*_*ail 2 python encoding

我尝试在 Python 中读取 CSV 文件,但第一行中的第一个元素是这样读取的0,而奇怪的字符不在文件中,它只是一个简单的0. 这是我使用的代码:

matriceDist=[]
file=csv.reader(open("distanceComm.csv","r"),delimiter=";")
for row in file:
    matriceDist.append(row)
print (matriceDist)
Run Code Online (Sandbox Code Playgroud)

小智 12

I had this same issue. Save your excel file as CSV (MS-DOS) vs. UTF-8 and those odd characters should be gone.


小智 8

按如下方式打开文件时指定字节顺序标记解决了我的问题:

open('inputfilename.csv', 'r', encoding='utf-8-sig')