I tried to print some records off "the file" in csv format, but it keeps showing the error in Title. Yes i tried to change encoding to utf8, utf-16, as well as utf-16-le using notepad++. it still doesn't work. Please help :c
This is the input code:
import csv
f=open("thefile.csv", 'r', encoding="ansi")
reader=csv.reader(f)
covaxin=0
covishield=0
c1="Covaxin"
c2="Covishield"
a="41 - 60 years"
for row in reader:
if (row[3]==a):
for bro in reader:
if (bro[4]==c1):
covaxin+=1
print("Covaxin: ", covaxin)
elif (bro[4]==c2):
covishield+=1
print("Covishield:", covishield)
else:
print("error")
f.close()
and this is the output:
Traceback (most recent call last):
File "C:\Users\admin\Desktop\Reader Program.py", line 13, in <module>
for row in reader:
_csv.Error: line contains NUL
The error message means that the cvs module is reading a NUL character from the file, ie a character with code 0. The most obvious reasons for that would be that the file actually contains such a character (which means that the file is broken), or that the file is encoded with a 16-bit encoding (such as UTF-16) but you are reading it with an 8-bit encoding (which would make every second character a NUL character, assuming the file keeps to the English alphabet). You must find out what encoding the file is in, and give the appropriate parameter to open
. To check whether there is a NUL character even with the proper encoding chosen, you could read the entire file as a string and use Python's string manipulation functions to search for '\ '
characters.
This thread has several potentially useful code snippets and hints for looking at what's in the file.
Interestingly, it looks like this error will be gone in a future version of Python.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.