user411103
user411103

Reputation:

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). 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?

Upvotes: 1

Views: 975

Answers (1)

jezrael
jezrael

Reputation: 862851

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:

sep

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

Delimiter to use. If sep is None, will try to automatically determine this. 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. Regex example: '\r\t'.

Upvotes: 4

Related Questions