为什么我的python文本搜索对CSV文件中的某些行正确工作,而对另一些行则不正确?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (65)

我编写了一个python脚本,它打开并读取具有以下结构的CSV文件

ID|Search Company|Past Job Title 1|Past Job Title 2|Past Job Title 3|

然后,它用以下结构编写一个CSV文件

ID|Search Company|Consolidated Company| Past Job Title 1|Past Job Title 2|Past Job Title 3|

我还有下面的python代码

input_filename = 'filename.csv'

input_headers = ['ID',
                 'Search Company',
                 'Past Job Title 1',
                 'Past Job Title 2',
                 'Past Job Title 3']

output_headers = ['ID',
                 'Search Company',
                 'Consolidated Company',
                 'Past Job Title 1',
                 'Past Job Title 2',
                 'Past Job Title 3']



#set up the file writer
out_employees_file = open('consolidated_' + input_filename, 'wb')
employees_writer = unicodecsv.DictWriter(out_employees_file, 
lineterminator='\n', escapechar='\\', fieldnames=output_headers)

#set up the file reader
employees_file = open(input_filename, 'r', encoding='utf-8')
employees_reader = csv.reader(employees_file, delimiter=',')


for row in employees_reader:
    consolidated_name = ''
    if 'Searched Company' != row[1]:
        if row[1] in row[2]:
            consolidated_name = row[2]

        if row[1] in row[3]:
            consolidated_name = row[3]

        if row[1] in row[4]:
            harmonized_name = row[4]

    employees_writer.writerow({'ID': row[0],
                           'Consolidated Company':consolidated_name,
                           'Past Job Title 1':row[2],
                           'Past Job Title 2':row[3],
                           'Past Job Title 3':row[4]})
employees_file.flush()
employees_file.close()

我的脚本运行,问题是它的行为只有在读取CSV文件的某些行时才像预期的那样。出于某种原因,Python只能在CSV文件的一些行中找到我要查找的公司名称。

我会给出一个输入文件样本。

ID|Search Company|Past Job Title 1|Past Job Title 2|Past Job Title 3|
 1|Good Company  |Bad Company     | Horrid Cmp LLC |Good Company    |
 2|Good Company  |Good Company    | Rotten Company |Stupid Company  |
 3|Good Company  |Stinky Company  | Good Company   |Company Malo    |

这是我得到的输出

ID|Search Company|Consolidated Company|Past Job Title 1|Past Job Title 2|Past Job Title 3|
 1|Good Company  |                    |Bad Company     | Horrid Cmp LLC |Good Company    |
 2|Good Company  |Good Company        |Good Company    | Rotten Company |Stupid Company  |
 3|Good Company  |                    |Stinky Company  | Good Company   |Company Malo    |

我应该在IntegrationCompany列中为输出文件的每一行获得一个值,因为‘Good Company’显示在文件的每一行中。然而,我实际上看到的是,我在一些行中只得到一个值。

我还没有弄清楚为什么我的脚本对输入文件的某些行有效,但是对于输入文件的其他行却失败了。我会认为我的脚本要么适用于所有的东西,要么失败了,但它没有,为什么呢?

提问于
用户回答回答于

我修改了你的代码,它起作用了:

import csv
import unicodecsv

input_filename = 'test.csv'

input_headers = ['ID',
                 'Search Company',
                 'Past Job Title 1',
                 'Past Job Title 2',
                 'Past Job Title 3']

output_headers = ['ID',
                  'Search Company',
                  'Consolidated Company',
                  'Past Job Title 1',
                  'Past Job Title 2',
                  'Past Job Title 3']

#set up the file writer
out_employees_file = open('consolidated_' + input_filename, 'wb')
employees_writer = unicodecsv.DictWriter(out_employees_file,
lineterminator='\n', escapechar='\\', fieldnames=output_headers)

#set up the file reader
employees_file = open(input_filename, 'r', encoding='utf-8')
employees_reader = csv.reader(employees_file, delimiter=',')

rows = []

for row in employees_reader:
    consolidated_name = ''
    if 'Search Company' != row[1]:
        if row[1] in row[2]:
            consolidated_name = row[2]

        if row[1] in row[3]:
            consolidated_name = row[3]

        if row[1] in row[4]:
            consolidated_name = row[4]
        r = {'ID': row[0],
             'Search Company': row[1],
             'Consolidated Company': consolidated_name,
             'Past Job Title 1': row[2],
             'Past Job Title 2': row[3],
             'Past Job Title 3': row[4]}
        rows.append(r)
employees_writer.writerows(rows)

你没有在你的版本(搜索公司)中写入第二行,并且至少在if条件下有一个类型:

if 'Searched Company' != row[1]:

我想,这就是你出问题的主要原因。此外,您还应该收集行并使用.writerows()编写它们。

我的输出:

1,Good Company,Good Company,Bad Company,Horrid Cmp LLC,Good Company
2,Good Company,Good Company,Good Company,Rotten Company,Stupid Compny
3,Good Company,Good Company,Stinky Company,Good Company,Company Malo

扫码关注云+社区

领取腾讯云代金券