Parsing CSV file in pandas with commas in last column

前端 未结 1 864
没有蜡笔的小新
没有蜡笔的小新 2021-01-19 02:05

I\'m stuck with some poorly formatted CSV data that I need to read into a Pandas dataframe. I cannot change how the data is being recorded (it\'s coming from someplace else)

1条回答
  •  野的像风
    2021-01-19 02:26

    Fix the csv, then proceed normally:

    import csv
    with open('path/to/broken.csv', 'rb') as f, open('path/to/fixed.csv', 'wb') as g:
        writer = csv.writer(g, delimiter=',')
        for line in f:
            row = line.split(',', 2)
            writer.writerow(row)
    

    import pandas as pd
    df = pd.read_csv('path/to/fixed.csv')
    

    0 讨论(0)
提交回复
热议问题