python-如果行中包含特定值,则读取行并将其写入文件

时间:2018-08-28 00:22:08

标签: python csv

我正在读取一个csv,我只想将在列中具有特定值的行写入另一个输出csv。

这是我的代码:

import csv
import pandas as pd

with open('contribs_01.csv', 'r') as file:
    csv_reader = csv.DictReader(file)

    with open('01_output.csv', 'w') as new_file:
        select_fields = ['contributorPersentTypeCd', 'contributorNameLast', 'contributorNameFirst', 'contributorStreetCity', 'contributorStreetStateCd', 'contributorStreetPostalCode', 'contributorOccupation', 'contributorEmployer', 'filerName', 'contributionAmount']

        csv_writer = csv.DictWriter(new_file, fieldnames=select_fields, extrasaction='ignore')

        csv_writer.writeheader()

        for line in csv_reader:
            if pd.np.where(csv_writer.contributorPersentTypeCd.str.contains('INDIVIDUAL'):
                csv_writer.writerow(line)

这是我得到的错误:

  File "01_donor_read.py", line 17
    if pd.np.where(csv_writer.contributorPersentTypeCd.str.contains('INDIVIDUAL'):
                                                                                 ^
SyntaxError: invalid syntax

0 个答案:

没有答案