Get the App
SLTechnology News&Howtos  ›  Development  › 

How to solve the csv read error caused by python coding format

Shulou Source: shulou.com Published: 2022-05-31 18:10:29 09月16日 Update

This article mainly explains the "python encoding format caused by csv reading errors how to solve", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in-depth, together to study and learn "python coding format leads to csv read errors how to solve" it!

Python encoding format causes csv read error

This article records the two problems I encountered today (csv.reader and pandas.csv_read) of python rookies:

Pandas module "CParserError: Error tokenizing data. C error: Expected 1 fields in line 4, saw 2" error

Csv module "line contains NULL byte" error

Today, I was negligent in dealing with the data, and I was lazy to copy the data to xlsx to save it, and then directly modified the file suffix to .csv to prepare for reading. Then there was a problem when running the algorithm to read the data.

Import pandas as pdpath = 'water30.csv'df=pd.read_csv (path)

Note: the last two lines can be written as df=pd.read_csv ('water30.csv').

But because read_csv itself has a lot of parameters (although not here), it is better to write as path.

This will result in an error CParserError: Error tokenizing data. C error: Expected 1 fields in line 4, saw 2

I have looked up many solutions on the Internet, because there are many parameters of read_csv, so each has its own words, and what I encounter here should be just one of them, which has been searched for a long time without any results. Until I read the code of the module _ csv.c here, I found that there can be no "\ 0" in the file, so the csv file cannot be unicode-encoded, it can be ANSI.

The result of changing my suffix directly is that I was prompted when I clicked on the .csv to open it:

That is, changing the suffix here does not get the file format right. So I chose "Save as" to change the file format to

After that, the reading will not be wrong.

Note: there is a question unsolved, that is, I "directly changed the suffix to get the .csv" I opened it with a notepad to check, the code is ANSI ah. Then I don't know why I reported it wrong. But the problem was temporarily solved.

The format now read is

It's a structure.

In addition, for: csv module "line contains NULL byte" error. The cause of the problem and the solution are the same as above, such as

Import csvcsvfile=file ('water30.csv','rb') reader = csv.reader (csvfile) for line in reader: print linecsvfile.close ()

Error report: Error: line contains NULL byte

After correction, the format of the read data is list, as follows

['1th,' 2th, '2th,' 1th,'2']

['1','1','1','2','2']

['1th,' 2th, '1th,' 1th,'1']

['1mm,' 1pm, '1pm,' 2']

['1','1','1','2','2']

['1','1','1','2','2']

['0.697,' 0.744, '0.634,' 0.403, '0.481']

['0.46,' 0.376, '0.264,' 0.237, '0.149]

['1','1','1','1']

Common errors in reading csv by pandas and their solutions 1) the first kind of error

Error prompt:

Pandas.errors.ParserError: Error tokenizing data. C error: Expected 1 fields in line 121, saw 2

Solution:

Import pandas as pddata = pd.read_csv (inputfile, encoding='utf-8',header=None,sep ='\ t') 2) the second error

Error prompt:

Pandas.errors.ParserError: Error tokenizing data. C error: EOF inside string starting at line 15945

Solution:

Import pandas as pdimport csvdf = pd.read_csv (csvfile, quoting=csv.QUOTE_NONE, encoding='utf-8') Thank you for your reading, the above is the content of "how to solve csv read errors caused by python encoding format". After the study of this article, I believe you have a deeper understanding of how to solve this problem caused by csv reading errors caused by python coding format, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Errors formats codes files problems suffixes data modules that is learning hints content parameters methods no two that is code yes methods Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Shulou Technology Huawei macOS Microsoft