You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Pandas读取CSV文件遇分词错误,无法绘制图表求助

解决Pandas读取CSV的分词错误问题

针对你遇到的Error tokenizing data. C error: Expected 5 fields in line 3, saw 6错误,以下是几种可行的解决方法:

方法1:指定正确的分隔符

这类错误常因CSV实际使用的分隔符不是默认逗号(,)导致,比如制表符(\t)或分号(;),可以尝试指定sep参数:

file = pd.read_csv('/content/drive/MyDrive/Dataset 1(no offset part1 plate 1).csv', sep='\t')
# 若分号分隔则用:
# file = pd.read_csv('/content/drive/MyDrive/Dataset 1(no offset part1 plate 1).csv', sep=';')

方法2:跳过错误行

如果仅少数行格式异常,可使用on_bad_lines='skip'跳过这些行:

file = pd.read_csv('/content/drive/MyDrive/Dataset 1(no offset part1 plate 1).csv', on_bad_lines='skip')

方法3:手动定义列名与列数

若CSV表头或数据列数不匹配,可手动指定列名强制按固定列数读取:

# 自定义列名(根据实际数据调整)
col_names = ['Col1', 'Current I_A1/A', 'Col3', 'Voltage U_B2/V', 'Col5']
file = pd.read_csv('/content/drive/MyDrive/Dataset 1(no offset part1 plate 1).csv', names=col_names, header=0)

方法4:排查原始数据格式

若不确定问题根源,可先读取几行原始文本确认格式:

with open('/content/drive/MyDrive/Dataset 1(no offset part1 plate 1).csv', 'r') as f:
    for i in range(5):
        print(repr(f.readline()))

通过输出能定位到具体哪一行存在多余分隔符,再针对性修正。

修正读取逻辑后,即可继续执行绘图代码:

x_axis = file['Current I_A1/A']
y_axis = file['Voltage U_B2/V']

# 绘制I-V曲线示例
plt.figure(figsize=(10,6))
plt.plot(x_axis, y_axis, 'b-', label='I-V Curve')
plt.xlabel('Current I_A1/A')
plt.ylabel('Voltage U_B2/V')
plt.title('Current vs Voltage')
plt.legend()
plt.show()

内容的提问来源于stack exchange,提问作者dutchrunner

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 18:27:09