我正在读取一个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