Multiple delimiters in single CSV file

前端 未结 3 358
情深已故
情深已故 2020-12-10 18:22

I have a CSV, which has got three different delimiters namely, \'|\', \',\' and \';\' between different columns.

How can I using Python parse this CSV ?

My

3条回答
  •  醉梦人生
    2020-12-10 18:52

    One easy way to achieve what you want is using pandas package, here's a little example:

    import pandas as pd
    import StringIO
    
    data = StringIO.StringIO("""a;b|c;
    2016-09-05 10:47:00|1,foo;
    2016-09-06 10:47:00;2;foo2;
    2016-09-07 10:47:00;3;foo3;""")
    
    df = pd.read_csv(data, sep='[;,|]', engine='python')
    for c in ['a', 'b', 'c']:
        print('-' * 80)
        print(df[c])
    

提交回复
热议问题