Skip to content Skip to sidebar Skip to footer

Strange Character While Reading A Csv File

I try to read a CSV file in Python, but the first element in the first row is read like that 0, while the strange character isn't in the file, its just a simple 0. Here is th

Solution 1:

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

Solution 2:

Specifying the byte order mark when opening the file as follows solved my issue:

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

Solution 3:

Just use pandas together with some encoding (utf-8 for example) is gonna be easier

import pandas as pd
df = pd.read_csv('distanceComm.csv', header=None, encoding = 'utf8', delimiter=';')
print(df)

Solution 4:

I don't know what your input file is. But since it has a Byte Order Mark for UTF-8, you can use something like this:

import codecs
matriceDist=[]
file=csv.reader(codecs.open('distanceComm.csv', encoding='utf-8'),delimiter=";")
for row in file:
    matriceDist.append(row)
print (matriceDist)

Post a Comment for "Strange Character While Reading A Csv File"