How do I get python to search a csv file for items in a dictionary then print out the entire excel row...Thanks

StackOverflow https://stackoverflow.com/questions/23613138

Question

Trying to get python to search a csv file for a specific phone number in a dictionary and then return the entire excel row. Thank you.

Example code:

import csv

def generateKnownReport(mypath, GKR):
    if GKR==True:
        with open("fileName.csv") as f:
            reader = csv.reader(f, delimiter=',')  
            file_one = list(reader)
        affiliate_phone_dict = {"xxx-xxx-xxxx":"Name 1","yyy-yyy-yyyy":"Name 2"}
        federal_phone_dict = {}
        for row in file_one:
            for each in row:
                search(affiliate_phone_dict,each)

def search(myDict, lookup):
    with open('KnownReport.csv','w') as f:
        for key, value in myDict.items():
            for value in key, value:
                if lookup in key:
                    f.write('{0}\n'.format(value))
                    f.write('{0}\n'.format(lookup))
     return 




GKR=True
mypath="November2013 T-Mobile Statement - Daily Detail.csv"
generateKnownReport(mypath, GKR)

Just to be clear I am trying to get python to write the entire line of the CSV file to the output file and not just the thing it was searching for. So for example, if I was searching in this csv file:

Date        Time Length Cost   Bill Category                                      Destination Number Destination City Origin Number Origin City Type

01/01/0001  10:37   3   $0.00   LOCAL AIRTIME, LONG DISTANCE and INTERNATIONAL CHARGES  xxx-xxx-xxxx    City Name   aaa-aaa-aaaa    City Name   Mobile

01/01/0001  10:37   10  $0.00   LOCAL AIRTIME, LONG DISTANCE and INTERNATIONAL CHARGES  yyy-yyy-yyyy    City Name   zzz-zzz-zzzz    City Name   Mobile

For the numbers xxx-xxx-xxxx and yyy-yyy-yyyy, I would want a line of code that prints out the entire row that those numbers were found on. Thank you.

Était-ce utile?

La solution

I'm not quite sure what you're asking, but if you want to print each row that contains any of the numbers in affiliate_phone_dict, this will do:

lookup = {'name1': 'xxx-xxx-xxxx',
          'name2': 'yyy-yyy-yyyy'}

with open('data.csv') as data_file, open('out.csv', 'w') as out_file:
    for row in data_file:
        if any(num in row for num in lookup.values()):
            out_file.write(row)

data.csv

Date Time Length Cost Bill Category Destination Number Destination City Origin Number OriginCity
01/01/0001  10:37   3   $0.00   LOCAL AIRTIME, LONG DISTANCE and INTERNATIONAL CHARGES  xxx-xxx-xxxx    City Name   aaa-aaa-aaaa    City Name   Mobile
01/01/0001  10:37   10  $0.00   LOCAL AIRTIME, LONG DISTANCE and INTERNATIONAL CHARGES  yyy-yyy-yyyy    City Name   zzz-zzz-zzzz    City Name   Mobile
01/01/0001  10:37   10  $0.00   LOCAL AIRTIME, LONG DISTANCE and INTERNATIONAL CHARGES  123-456-7890    City Name   zzz-zzz-zzzz    City Name   Mobile

out.csv

01/01/0001  10:37   3   $0.00   LOCAL AIRTIME, LONG DISTANCE and INTERNATIONAL CHARGES  xxx-xxx-xxxx    City Name   aaa-aaa-aaaa    City Name   Mobile
01/01/0001  10:37   10  $0.00   LOCAL AIRTIME, LONG DISTANCE and INTERNATIONAL CHARGES  yyy-yyy-yyyy    City Name   zzz-zzz-zzzz    City Name   Mobile
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top