Solve the error "ParserError: Error tokenizing data. C error: Expected 1 fields in line..." when pandas reads csv and tsv files

Change the way to read the file to

import pandas as pd
pd_data = pd.read_csv('./files.tsv')

Error, ParserError: Error tokenizing data. C error: Expected 1 fields in line...
Change the reading method to

documents = pd.read_csv('./files.tsv', sep='\t', header=0)

OK, problem solved!

read_csv() is a function used to read CSV files in the Pandas library, and its common parameters are as follows:

filepath_or_buffer---->CSV文件的路径或URL地址。
sep---->CSV文件中字段分隔符,默认为逗号。
delimiter---->CSV文件中字段分隔符,默认为None。
header---->指定哪一行作为列名,默认为0,即第一行。
names---->自定义列名,如果header=None,则可以使用该参数。
index_col---->用作行索引的列编号或列名。
usecols---->读取指定的列,可以是列名或列编号。
dtype---->指定每列的数据类型,可以是字典或者函数。
na_values---->用于替换缺失值的值。
skiprows---->跳过指定的行数。
skipfooter---->跳过文件末尾的指定行数。
nrows---->读取指定的行数。
parse_dates---->指定哪些列需要转换为日期类型。
infer_datetime_format---->尝试解析日期时间格式(提高效率)。
dayfirst---->将日期解析为“日--年”而不是“月--年”的格式。
encoding---->CSV文件的编码方式,默认为None,使用系统默认编码。
squeeze---->如果文件只包含一列,则返回Series对象而不是DataFrame对象。
thousands---->千位分隔符。
decimal---->小数点分隔符。

Guess you like

Origin blog.csdn.net/weixin_43479947/article/details/129586070