简体   繁体   English

熊猫:使用整个字符串作为CSV文件上的分隔符

[英]Pandas: use whole string as separator on CSV file

In my CSV file each row is separated with delimiter "$$$Field$$$" (simple string, is not a regex). 在我的CSV文件中,每行都用定界符"$$$Field$$$" (简单字符串,不是正则表达式)分隔。 I am trying to do the following, but it is ignoring the separator. 我正在尝试执行以下操作,但它忽略了分隔符。

df = pd.read_csv('filename.csv', sep='\b$$$Field$$$\b')

Any ideas? 有任何想法吗?

It seems you need escape $ by \\ : 看来您需要使用\\转义$

import pandas as pd
from pandas.compat import StringIO

temp=u"""Food$$$Field$$$Taste
Apple$$$Field$$$a
Banana$$$Field$$$b"""
#after testing replace 'StringIO(temp)' to 'filename.csv'
df = pd.read_csv(StringIO(temp), sep='\$\$\$Field\$\$\$',engine='python')
print (df)
     Food Taste
0   Apple     a
1  Banana     b

read_csv docs: read_csv文件:

sep 九月

: str, defaults to ',' for read_csv(), \\t for read_table() :str,对于read_csv()默认为',',对于read_table()默认为\\ t

Delimiter to use. 要使用的定界符。 If sep is None, will try to automatically determine this. 如果sep为None,将尝试自动确定。 Separators longer than 1 character and different from '\\s+' will be interpreted as regular expressions , will force use of the python parsing engine and will ignore quotes in the data. 大于1个字符且与'\\ s +'不同的分隔符将被解释为正则表达式 ,将强制使用python解析引擎,并且将忽略数据中的引号。 Regex example: '\\r\\t'. 正则表达式示例:“ \\ r \\ t”。

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

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