简体   繁体   中英

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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