简体   繁体   English

Python pandas read_csv 带自定义分隔符

[英]Python pandas read_csv with custom separator

I have a CSV file where columns are separated using a non-standard symbol (||/).我有一个 CSV 文件,其中列使用非标准符号 (||/) 分隔。

df = pd.read_csv('data_analyst_assignment.csv',sep='||/', engine='python')

This throws an error:这会引发错误:

ParserError: Expected 61 fields in line 3, saw 68. Error could possibly be due to quotes being ignored when a multi-char delimiter is used. ParserError:预期第 3 行中有 61 个字段,看到 68 个。错误可能是由于使用多字符分隔符时忽略引号引起的。

Can you please help me how to read this file?你能帮我如何阅读这个文件吗?

From .read_csv().read_csv()

sep :str, default ',': Delimiter to use. sep :str, default ',': 要使用的分隔符。 ... 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. ...此外,长度超过 1 个字符且不同于 '\s+' 的分隔符将被解释为正则表达式,并且还将强制使用 Python 解析引擎。

And || is special char in regex grammar (means OR) so you need to escape it, so you need是正则表达式语法中的特殊字符(表示 OR)所以你需要转义它,所以你需要

df = pd.read_csv('data_analyst_assignment.csv',sep='\|\|/', engine='python')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM