尝试让python在字典中搜索csv文件中的特定电话号码,然后返回整个excel行。谢谢。
示例代码:
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)为了明确起见,我试图让python将CSV文件的整行写入输出文件,而不仅仅是它正在搜索的内容。例如,如果我在这个csv文件中搜索:
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对于数字xxx-xxx-xxxx和,我需要一行代码打印出这些数字所在的整个行。谢谢。
发布于 2014-05-13 17:24:02
我不太清楚您在问什么,但是如果您想打印包含affiliate_phone_dict中任何数字的每一行,可以这样做:
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 Mobileout.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 Mobilehttps://stackoverflow.com/questions/23613138
复制相似问题