What you will learn?
pandas read_csv escape commas and double qoutes
Prepare datas
# test.csv or test.txt
"1","123","4","\"data\""
"test","123","4","if(\"data\" = \"<test>\", (10*24))"
Wrong-way
import pandas as pd
datas = pd.read_csv('test.txt', header=None, skip_blank_lines=True)
You got
pandas\_libs\parsers.pyx in pandas._libs.parsers.TextReader.read()
pandas\_libs\parsers.pyx in pandas._libs.parsers.TextReader._read_low_memory()
pandas\_libs\parsers.pyx in pandas._libs.parsers.TextReader._read_rows()
pandas\_libs\parsers.pyx in pandas._libs.parsers.TextReader._tokenize_rows()
pandas\_libs\parsers.pyx in pandas._libs.parsers.raise_parser_error()
ParserError: Error tokenizing data. C error: Expected 4 fields in line 2, saw 5
Right way
import pandas as pd
datas = pd.read_csv('test.txt', header=None, skip_blank_lines=True, escapechar='\\')
Digression
Many people on the Internet encounter this problem and add a parameter: error_bad_Lines = false (tested, the second row will be lost for the above data). If the amount of data is not large, check the method of the specified row: cat – N filename | head – N end_line_no| tail -n +start_line_no
Read More:
- [How to Fix]pandas.errors.ParserError: Error tokenizing data
- How to Fix pandas.errors.ParserError Error tokenizing data C error Buffer overflow caught
- Python opens the table and appears pandas.errors.ParserError: Error tokenizing data. C error:
- Pandas Read csv Error tokenizing data. C error: Expected 18 fields in line 173315, saw 20
- Error reading file by pandas pandas.errors.EmptyDataError: no columns to parse from file
- [Solved] Python Pandas Read Error: OSError: initializing from file failed
- Python: How to Reshape the data in Pandas DataFrame
- [Solved] ParserError: NULL byte detected. This byte cannot be processed in Python‘s native csv library
- How to Solve Python Pandas Read or Import Files Error
- Pandas Error: ValueError: setting an array element with a sequence.
- [Solved] AttributeError: module ‘pandas‘ has no attribute ‘rolling_count‘
- Python Pandas Typeerror: invalid type comparison
- Pandas uses str.contains to filter error [How to Solve]
- [Solved] Pandas rename Error: ValueError: operands could not be broadcast together with shapes (1,2) (3,) (1,2)
- Python Pandas Error: KeyError: 0 [How to Solve]
- Python+ Pandas + Evaluation of Music Equipment over the years (Notes)
- Python Pandas Error: No module named ‘openpyxl‘
- How to Solve jupyter notebook Read CSV files Error
- [Solved] Pandas dataframe merge error: Different types cannot be merged
- Pandas ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.an