【问题标题】:How do I get python to search a csv file for items in a dictionary then print out the entire excel row...Thanks如何让 python 在 csv 文件中搜索字典中的项目,然后打印出整个 excel 行...谢谢
【发布时间】:2014-05-12 15:30:50
【问题描述】:

试图让 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 和 yyy-yyy-yyyy,我想要一行代码打印出找到这些数字的整行。谢谢。

【问题讨论】:

  • 您有什么具体问题吗?什么在您的代码中有效和无效?如果抛出错误,能否提供(完整的)回溯?
  • 是的,对不起。所以我的问题是如何对其进行编码以使python打印找到值的整个excel行。当我运行它时,如果它在文件中找到它,它会在一定程度上写入名称和编号。但是,如前所述,我试图让它打印整个 excel 文件行。谢谢。
  • 我不确定我是否理解你想要做什么。您可以使用示例 csv 文件和您的预期输出来更新您的问题吗?如果您也可以使用上面的评论更新您的问题,那就太好了,这样未来的访问者就不必阅读 cmets 来理解问题和答案:)
  • 我很抱歉,我试图解释自己,但事实证明我的观点很难理解。谢谢:)
  • 任何帮助将不胜感激。谢谢!

标签: excel csv python-3.x dictionary report


【解决方案1】:

我不太确定你在问什么,但如果你想打印包含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   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

【讨论】:

  • 我试过了,但这不起作用。这是我现在拥有的代码:affiliate_phone_dict = {"xxx-xxx-xxxx":"Name 1","yyy-yyy-yyyy":"Name 2"} with open("November2013 T-Mobile Statement - Daily Detail EDITED.csv") as dataFile: ##reader = csv.reader(f, delimiter=',') ##file_one = list(reader) for row in dataFile: for each in row: search(affiliate_phone_dict, each, row) def search(myDict, lookup, row): with open('KnownReport.csv','w') as f: if any(num in row for num in myDict.values()): f.write(row)
  • 我为上面的代码道歉,但我不擅长在这个网站上格式化。
  • @nCodeProgrammer 很抱歉,SO 不是您的个人调试器.. :) 试试我的代码,确认它自行运行,然后尝试实现小的一次一个。使用调试器(首选)或插入打印语句,以便您知道代码中发生了什么。埃里克·利珀特 (Eric Lippert) 的 blog post 是一本很好的读物。我建议你阅读它。
猜你喜欢
  • 1970-01-01
  • 2018-07-11
  • 1970-01-01
  • 2015-09-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多