Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change Column Headers using DictWriter

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

like image 430
eklone 1 Avatar asked Dec 11 '19 18:12

eklone 1


People also ask

How to write header in CSV file using dictwriter?

Pass in the field (column) names when instantiating csv.DictWriter write the header with writeheader () before writing any rows You may also download the above code here. (Note that we used utf-8-sig as the encoding, so that it opens well as a UTF-8 encoded CSV in Microsoft Excel.

How do I rename a dictionary in a column header?

Create a dictionary and set key = old name, value= new name of columns header. Assign the dictionary in columns. Call the rename method and pass columns that contain dictionary and inplace=true as an argument.

How to rename multiple column headers in a pandas Dataframe with dictionary?

Python - How to rename multiple column headers in a Pandas DataFrame with Dictionary? To rename multiple column headers, use the rename () method and set the dictionary in the columns parameter. At first, let us create a DataFrame −

What is column header in Excel?

How to Use Column Header in Excel? Column Header is a very important part of excel as we work on different types of Tables in excel every day. Column Headers basically tell us the category of the data in that column to which it belongs.


Video Answer


1 Answers

Instead of writeheader, use writerow with a dictionary mapping the old headers on to the new ones. You can then write the other rows as before.

QATargetHeaders = {'FNAME': 'First Name','LNAME': 'Last Name'}
FinalTargetHeaders = {'PROJECT_CODE': 'PROJECT ID','PHONE_NUMBER': 'DEVICE1'}
...
    writer = csv.DictWriter(csv_output, fieldnames=QATargetHeaders, extrasaction='ignore')
    writer.writerow(QATargetHeaders)  # instead of writeheader
    for line in reader:
        writer.writerow(line)
...

(As of Python 3.7 dictionaries preserve order so the columns will appear in the right order. If using earlier versions of Python you will need to either create a separate list of the field names, or use an OrderedDict, to ensure that the columns appear in the CSV file in the right order.)

like image 188
Stuart Avatar answered Oct 22 '22 22:10

Stuart