Python Forum
Thread Rating:
  • 3 Vote(s) - 2.33 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Grouping csv by name
#6
but the second csv record with name john will overwrite the first.
import csv
import os
 
# I need following for my virtual environment, you can remove if
# running python from same directory as csv file.
os.chdir(os.path.abspath(os.path.dirname(__file__)))

result = []

plain_dict = {}
# To get flat normal dict, can just use dict(ordered_dict)
# but to get name as key:
with open("student.csv", "r") as csv_ledger:
    name = None
    reader = csv.DictReader(csv_ledger)
    for row in csv.DictReader(csv_ledger, skipinitialspace=True):
        for k, v in row.items():
            if k == 'name':
                name = v
                plain_dict[name] = {}
            else:
                plain_dict[name][k] = v
    print(plain_dict)
and since there are two csv rows with sane name, second overwrites first.
result:
{'john': {'email': 'hello.com', 'date': '12/08/18', 'phone': '123456'}}
but can be saved with json.dump

code again with json dump and reread json:
import csv
import json
import os
 
# I need following for my virtual environment, you can remove if
# running python from same directory as csv file.
os.chdir(os.path.abspath(os.path.dirname(__file__)))

result = []

plain_dict = {}
# To get flat normal dict, can just use dict(ordered_dict)
# but to get name as key:
with open("student.csv", "r") as csv_ledger:
    name = None
    reader = csv.DictReader(csv_ledger)
    for row in csv.DictReader(csv_ledger, skipinitialspace=True):
        for k, v in row.items():
            if k == 'name':
                name = v
                plain_dict[name] = {}
            else:
                plain_dict[name][k] = v

    print(f'plain_dict: {plain_dict}')
    with open('student.json', 'w') as jp:
        json.dump(plain_dict, jp)
    
    # reload json to make sure ok
    with open('student.json') as jp:
        new_dict = json.load(jp)
    print(f'new_dict: {new_dict}')
output:
Output:
plain_dict: {'john': {'email': 'hello.com', 'date': '12/08/18', 'phone': '123456'}} new_dict: {'john': {'email': 'hello.com', 'date': '12/08/18', 'phone': '123456'}
Reply


Messages In This Thread
Grouping csv by name - by terrydidi - Jan-13-2019, 09:50 PM
RE: Grouping csv by name - by Larz60+ - Jan-13-2019, 10:07 PM
RE: Grouping csv by name - by terrydidi - Jan-13-2019, 10:12 PM
RE: Grouping csv by name - by Larz60+ - Jan-13-2019, 10:35 PM
RE: Grouping csv by name - by terrydidi - Jan-13-2019, 11:05 PM
RE: Grouping csv by name - by Larz60+ - Jan-14-2019, 12:08 AM
RE: Grouping csv by name - by terrydidi - Jan-14-2019, 12:29 AM
RE: Grouping csv by name - by scidam - Jan-14-2019, 01:51 AM
RE: Grouping csv by name - by perfringo - Jan-14-2019, 09:27 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Grouping Data based on 30% bracket purnima1 4 1,255 Mar-10-2023, 07:38 PM
Last Post: deanhystad
  Grouping and sum of a list of objects Otbredbaron 1 3,280 Oct-23-2021, 01:42 PM
Last Post: Gribouillis
  Grouping and summing of dataset jef 0 1,675 Oct-04-2020, 11:03 PM
Last Post: jef
  Grouping algorithm riccardoob 7 3,107 May-19-2020, 01:22 PM
Last Post: deanhystad
  Help Grouping by Intervals on list paul41 1 2,188 Dec-03-2019, 09:43 PM
Last Post: michael1789
  Grouping a list of various time into intervals paul41 1 3,823 Nov-24-2019, 01:47 PM
Last Post: buran
  resample grouping pr0blem olufemig 1 2,004 Nov-06-2019, 10:45 PM
Last Post: Larz60+
  Splitting lines ang grouping three at once samsonite 5 2,834 Jun-21-2019, 05:19 PM
Last Post: ichabod801
  Function for grouping variables Scott 1 2,742 Nov-13-2018, 03:01 AM
Last Post: ichabod801
  Python grouping program GhostZero199 2 3,360 Jul-18-2017, 12:44 PM
Last Post: sparkz_alot

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020