👻 Check our latest review to choose the best laptop for Machine Learning engineers and Deep learning tasks!
I"m working with some CSV files, with the following code:
reader = csv.reader(open(filepath, "rU"))
try:
for row in reader:
print "Row read successfully!", row
except csv.Error, e:
sys.exit("file %s, line %d: %s" % (filename, reader.line_num, e))
And one file is throwing this error:
file my.csv, line 1: line contains NULL byte
What can I do? Google seems to suggest that it may be an Excel file that"s been saved as a .csv improperly. Is there any way I can get round this problem in Python?
== UPDATE ==
Following @JohnMachin"s comment below, I tried adding these lines to my script:
print repr(open(filepath, "rb").read(200)) # dump 1st 200 bytes of file
data = open(filepath, "rb").read()
print data.find("x00")
print data.count("x00")
And this is the output I got:
"xd0xcfx11xe0xa1xb1x1axe1x00x00x00x00x00x00x00x00 .... <snip>
8
13834
So the file does indeed contain NUL bytes.
👻 Read also: what is the best laptop for engineering students?
We hope this article has helped you to resolve the problem. Apart from Python CSV error: line contains NULL byte, check other code Python module-related topics.
Want to excel in Python? See our review of the best Python online courses 2023. If you are interested in Data Science, check also how to learn programming in R.
By the way, this material is also available in other languages:
- Italiano Python CSV error: line contains NULL byte
- Deutsch Python CSV error: line contains NULL byte
- Français Python CSV error: line contains NULL byte
- Español Python CSV error: line contains NULL byte
- Türk Python CSV error: line contains NULL byte
- Русский Python CSV error: line contains NULL byte
- Português Python CSV error: line contains NULL byte
- Polski Python CSV error: line contains NULL byte
- Nederlandse Python CSV error: line contains NULL byte
- 中文 Python CSV error: line contains NULL byte
- 한국어 Python CSV error: line contains NULL byte
- 日本語 Python CSV error: line contains NULL byte
- हिन्दी Python CSV error: line contains NULL byte
Milan | 2023-01-28
I was preparing for my coding interview, thanks for clarifying this - Python CSV error: line contains NULL byte in Python is not the simplest one. Will use it in my bachelor thesis
Rome | 2023-01-28
StackOverflow is always a bit confusing 😭 Python CSV error: line contains NULL byte is not the only problem I encountered. Checked yesterday, it works!
Tallinn | 2023-01-28
Simply put and clear. Thank you for sharing. Python CSV error: line contains NULL byte and other issues with code Python module was always my weak point 😁. I am just not quite sure it is the best method