Load a CSV with different row lengths

Miki Tebeka miki.tebeka at gmail.com
Wed Jul 30 07:00:22 CEST 2014


> I should've mentioned that I want to import my csv as a data frame or numpy array or as a table.
If you know the max length of a row, then you can do something like:
    def gen_rows(stream, max_length):
        for row in csv.reader(stream):
            yield row + ([None] * (max_length - len(line))

    max_length = 10
    with open('data.csv') as fo:
        df = pd.DataFrame.from_records(gen_rows(fo, max_length))


More information about the Python-list mailing list