这其实比你想象的要简单。
def write_to_csv(column_variant='Ticker'):
# Use a with block so the file auto-closes
with open('output.csv', 'w', encoding='utf-8', newline='') as csv_file:
data_writer = csv.DictWriter(csv_file, fieldnames=['fund', column_variant, 'Price, 'Date'])
data_writer.writeheader()
for row in input_file:
data_writer.writerow({'fund': fund, column_variant: row[0].value, 'Price': row[1].value, 'Date': row[2].value)