如何进行嵌套循环来搜索文件中的字符串

时间:2013-06-27 02:46:31

标签: python csv

我有2个包含数千个数据的excel文件。我想从file1获取行并搜索整个文件2并将重复输出到file3。

   file 1                        file2                      file3
 abc.bcg@gmail.com            abc.bcg_12253              abc.bcg_12253
 bcg.abc@gmail.com            efx.rfz_12345              def.xyz_08345
  def.xyz@gmail.com           wqr.qtf_34567
                              zxc.mnb_98764
                              def.xyz_08345

       FileReader = csv.DictReader(f)
            for row in FileReader:
                    emailLegalFile = row['email']
                    name_emailFile = emailFile[:emailLegalFile.find('@')]
                    print name_emailLegalFile

                    #with open(inputfile, 'rb') as d:
                    inputFileReader = csv.DictReader(d)
                    for r in inputFileReader:
                            if name_emailFile in  r['google_email']:
                                    date = r['date']
                                    time = r['time']
                                    t_format = r['format']
                                    file_size = r['file_size']
                                    google_email = r['google_email']

                                    #writer = csv.writer(w)

                                    #dic = {'date': date, 'time':time,'format':t_format,'file_size':file_size, 'google_email':google_email}

                                    #writer.writerow(dic)
                                    list = [date,time,t_format,file_size,google_email]

                                    with open('result.csv','a') as e:
                                            writer_1 = csv.writer(e,delimiter=',',quotechar='|', quoting=csv.QUOTE_MINIMAL)
                                            writer_1.writerow(list)

File2有5列,但只想与第5列匹配。

我得到的输出只是第一个值,即abc.bcg_12253。

请帮我解决这个问题。

谢谢

0 个答案:

没有答案