简体   繁体   English

如何在 pandas 中读取带有多个定界符的 csv 文件

[英]How to read a csv file with muplitiple delimiter in pandas

I have a csv file with delimiter (dot and underscore) and I am using sep='_.'我有一个带有分隔符(点和下划线)的 csv 文件,我正在使用 sep='_.' in read_csv but it is not taking dot as sep while reading.在 read_csv 中,但它在阅读时没有将点作为 sep。

input jks_12034.45_89.12输入 jks_12034.45_89.12

output jks 12034 45 89 12 output jks 12034 45 89 12

As stated in the documentation文档中所述

separators longer than 1 character and different from '\s+' will be interpreted as regular expressions超过 1 个字符且不同于 '\s+' 的分隔符将被解释为正则表达式

If you use sep="_\."如果你使用sep="_\." it will only match a point where youhave both an underscore AND a dot.它只会匹配同时具有下划线点的点。

If you want to split on unserscore OR dot use sep="\.|_" or sep="[_\.]"如果你想在 unserscore点上拆分,请使用sep="\.|_"sep="[_\.]"

Use engine='python' and sep=r'[_.]' as parameters of pd.read_csv :使用engine='python'sep=r'[_.]'作为pd.read_csv的参数:

df = pd.read_csv('data.csv', sep=r'[_.]', engine='python', header=None)
print(df)

# Output
     0      1   2   3   4
0  jks  12034  45  89  12

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

相关问题 如何使用熊猫在最后一个字段中存在分隔符的情况下读取CSV文件? - How to use Pandas to read CSV file with delimiter existing in the last field? 如何使用Pandas读取CSV并且只在没有Sep或Delimiter的情况下将其读入1列 - How to read a CSV with Pandas and only read it into 1 column without a Sep or Delimiter Pandas:读取多字符分隔符 csv 文件? - Pandas: Read multi-char delimiter csv file? 熊猫 read_csv。 如何在换行符之前忽略分隔符 - pandas read_csv. How to ignore delimiter before line break 如何在 pandas read_csv() 中指定行分隔符? - How to specify a row delimiter in pandas read_csv()? 如何将 csv 文件读入熊猫,跳过行直到某个字符串,然后选择第一行作为标题和分隔符作为 | - How to read csv file into pandas, skipping rows until a certain string, then selecting first row after as header and delimiter as | 如何从多个分隔符值中将pandas中的csv文件读取为两列 - How to read csv file in pandas as two column from multiple delimiter values 如何读取 pandas 中缺少分隔符的 csv(或 - 带有其他分隔符) - How to read a csv in pandas with a missing delimiter (or - with additional delimiters) Pandas:是否可以使用多个符号分隔符读取 CSV? - Pandas: is it possible to read CSV with multiple symbols delimiter? 带有分隔符的Python熊猫read_csv - Python pandas read_csv with " delimiter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM