'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='_.' in read_csv but it is not taking dot as sep while reading.

input jks_12034.45_89.12

output jks 12034 45 89 12



Solution 1:[1]

As stated in the documentation

separators longer than 1 character and different from '\s+' will be interpreted as regular expressions

If you use 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="[_\.]"

Solution 2:[2]

Use engine='python' and sep=r'[_.]' as parameters of 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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Matteo Zanoni
Solution 2 Corralien