示波器生成的csv使用pandas下read_csv()打开报错 报错: ParserError: Error tokenizing data. C error

示波器生成的csv报错

pandas直接使用示波器生成的csv,结果打不开并报错

报错: ParserError: Error tokenizing data. C error: Expected 2 fields in line 5, saw 17

data = pd.read_csv ( "G:/NEW.csv" )

执行代码。

原因:没有仔细阅读手册,以及对CSV的格式不够了解

解决方案:

data = pd.read_csv ( "G:/NEW.csv",sep='\t' )

CSV中的分割符分为逗号分隔和制表符(TAB)分隔,Pandas中read_csv设置了两种格式,默认逗号分隔符,设置为\t则试图以制表符模式分析

pandas官方文档

其中关于sep属性的描述

sep : str, defaults to ',' for read_csv(), \t for read_table()

Delimiter to use. If sep is None, the C engine cannot automatically detect the separator, but the Python parsing engine can, meaning the latter will be used and automatically detect the separator by Python’s builtin sniffer tool, csv.Sniffer. In addition, separators longer than 1 character and different from '\s+' will be interpreted as regular expressions and will also force the use of the Python parsing engine. Note that regex delimiters are prone to ignoring quoted data. Regex example: '\\r\\t'.

指定分隔符。如果不指定参数,则会尝试使用逗号分隔。分隔符长于一个字符并且不是‘\s+’,将使用python的语法分析器。并且忽略数据中的逗号。正则表达式例子:’\r\t’

参考资料1

参考资料2

这后面还有一个坑,即使打开了也会变成一个对象,用excel打开能观察到所有的数据都挤到了第一列。

解决方法:excel打开另存为逗号分隔符CSV文件,再用read_csv就可以了

急需点赞,各位看官有用的话请帮忙点赞

其他参考资料 Pandas 常见操作详解

^ _ ^
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值