Skip to content

Instantly share code, notes, and snippets.

@cyriac
Last active August 29, 2015 14:16

Revisions

  1. cyriac revised this gist May 11, 2015. 1 changed file with 3 additions and 15 deletions.
    18 changes: 3 additions & 15 deletions csv_to_list_of_dict.py
    Original file line number Diff line number Diff line change
    @@ -3,19 +3,7 @@
    mrows = []
    # Creates list of dict from csv
    with open('final.csv', mode='r') as infile:
    reader = csv.reader(infile)
    counter = 1
    header = None
    rows = []
    reader = csv.DictReader(infile)
    for row in reader:
    if counter == 1:
    header = row
    else:
    rows.append(row)
    counter = counter + 1

    for row in rows:
    mdict = {}
    for i in range(len(header)):
    mdict[header[i]] = row[i]
    mrows.append(mdict)
    mrows.append(row)
    header = row.keys()
  2. cyriac revised this gist Apr 24, 2015. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions csv_to_list_of_dict.py
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    import csv

    mrows = []
    # Creates list of dict from csv
    with open('final.csv', mode='r') as infile:
  3. cyriac created this gist Mar 3, 2015.
    19 changes: 19 additions & 0 deletions csv_to_list_of_dict.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    mrows = []
    # Creates list of dict from csv
    with open('final.csv', mode='r') as infile:
    reader = csv.reader(infile)
    counter = 1
    header = None
    rows = []
    for row in reader:
    if counter == 1:
    header = row
    else:
    rows.append(row)
    counter = counter + 1

    for row in rows:
    mdict = {}
    for i in range(len(header)):
    mdict[header[i]] = row[i]
    mrows.append(mdict)