-2

I have made a program that reads from a csv file, with this code;

reader = csv.DictReader(open('FakeNameSet.csv', 'r', encoding='utf-8-sig'))
customer_list = []
for line in reader:
    customer_list.append(line)

This code creates these types of ordered dictionaries from my csv file;

OrderedDict([('Number', '19'),
              ('Gender', 'female'),
              ('NameSet', 'Dutch'),
              ('GivenName', 'Özgül'),
              ('Surname', 'Overgaauw'),
              ('StreetAddress', 'Adriana Noorlandersingel 200'),
              ('ZipCode', '3065 HE'),
              ('City', 'Rotterdam'),
              ('EmailAddress', 'OzgulOvergaauw@superrito.com'),
              ('Username', 'Shrothem1971'),
              ('TelephoneNumber', '06-15253488')]),
 OrderedDict([('Number', '20'),
              ('Gender', 'female'),
              ('NameSet', 'Dutch'),
              ('GivenName', 'Gülseren'),
              ('Surname', 'Willigenburg'),
              ('StreetAddress', 'Dingspelstraat 28'),
              ('ZipCode', '9461 JE'),
              ('City', 'Gieten'),
              ('EmailAddress', 'GulserenWilligenburg@teleworm.us'),
              ('Username', 'Ressoare'),
              ('TelephoneNumber', '06-92433659')])]

Now I need to access the people in this list by name, but I have no idea how to access the name in this OrderedDict style. I've been trying for 2 hours and nothing seems to be working.

So basically, I have a list with a number of these ordered dictionaries and I want to search for a particular person in these dictionaries; so basically a function that works as follows;

Search_for_person("Gülseren"), and then it returns all the information for that person.

Alec
  • 8,529
  • 8
  • 37
  • 63
daan
  • 9
  • 2
  • 2
    **What** have you tried in that time though? A simple Google search for "OrderedDict access" yields exactly what you need. – meowgoesthedog Apr 23 '19 at 12:54
  • I followed the instructions given here; https://stackoverflow.com/questions/10058140/accessing-items-in-an-collections-ordereddict-by-index but what they have is not the same structure as I have right here, they make a single OrderedDict and search through that, but I have 20 of them and I just don't know what Im doing wrong – daan Apr 23 '19 at 13:04

1 Answers1

0

so, records are ordered dictionaries but you keep customers in a list. If you only search by name, changing customer_list to a dictionary should solve the problem:

customers = {}
for line in reader:
    customers['GivenName'] = line

# assuming GivenName is unique, search customers by given name:
customer = customers["Gülseren"]

If you need to search by other fields, I would recommend using pandas dataframes:

import pandas as pd

customers = pd.read_csv('FakeNameSet.csv', encoding='utf-8-sig')
# find customers by a given name:
# (doesn't assume uniqueness, you can get several rows)
customers[customers['GivenName] == "Gülseren"]
Marat
  • 15,215
  • 2
  • 39
  • 48