I am working on some code to produce two new CSV files from a input .txt file. The .txt file has 40 columns of data and I am writing two new csv files that only take in 2 columns from the .txt file each.
My code is working great and producing almost exactly what i need. The last step is renaming the original headers in the two new csv outputs.
Here is my code:
import csv
QATargetHeaders = ['FNAME','LNAME']
FinalTargetHeaders = ['PROJECT_CODE','PHONE_NUMBER']
with open('TEST.txt','r') as csv_input, open('Test_QA2.csv','w') as csv_output:
reader = csv.DictReader(csv_input, delimiter='^')
writer = csv.DictWriter(csv_output, fieldnames=QATargetHeaders, extrasaction='ignore')
writer.writeheader()
for line in reader:
writer.writerow(line)
with open('TEST.txt','r') as csv_input, open('Test_FINAL.csv','w') as csv_output:
reader = csv.DictReader(csv_input, delimiter='^')
writer = csv.DictWriter(csv_output, fieldnames=FinalTargetHeaders, extrasaction='ignore')
writer.writeheader()
for line in reader:
writer.writerow(line)
For TEST_QA2.csv, i need to change the headers:
FNAME = First Name
LNAME = Last Name
For TEST_FINAL.csv, i need to change the headers:
PROJECT_CODE = PROJECT ID
PHONE_NUMBER = DEVICE1
Can someone help me figure out how to rewrite the headers here? The TargetHeaders must be defined as the exact column names from the .txt file so my writer knows what to copy in. Thank you in advance