Load a CSV with different row lengths

Miki Tebeka miki.tebeka at gmail.com
Wed Jul 30 01:00:22 EDT 2014


Greetings,

> 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))


HTH,
Miki



More information about the Python-list mailing list