How to append csv data as a ROW to another existing csv and move to 1st row. When i try to append all the data is at the bottom of the first column

Question:

I have a csv with one row of data. It represents legacy headers that I am trying to append as 1 new row (or consider it as many columns) in a second csv. I need to compare the legacy header with the second csv’s current headers, so after i append the data from the first csv i want to move it so that it’s the first row too.

The issue right now is that when i append my data from the first csv it just all goes to the bottom of the first column.

See below for my code. How can i make it so that it takes the 1 row of data in my first csv and appends it to my second csv as ONE NEW ROW. After how would i move it so that it becomes the first row in my data (not as a header)

with open('filewith1row.csv', 'r', encoding='utf8') as reader:
    with open('mainfile.csv', 'a', encoding='utf8') as writer:
        for line in reader:
            writer.write(line)

Please help!! Thank you in advanced

Asked By: bananas

||

Answers:

You could use pandas to import the csv files, combine the two, and then overwrite the original mainfile.csv.

I have created some dummy data to demonstrate. Here are the test files that I used:

mainfile.csv

Fruit,Animals,Numbers
Apple,Cat,5
Banana,Dog,8
Cherry,Goat,2
Durian,Horse,4

filewith1row.csv

Fruta,Animales,NĂºmeros

This is the code that I used to combine the two CSVs.

Code:

import pandas as pd

mainfile = pd.read_csv('mainfile.csv', header=None)
one_liner = pd.read_csv('filewith1row.csv', header=None)

mainfile.loc[0.5]=one_liner.loc[0]
mainfile = mainfile.sort_index()       
mainfile.to_csv('mainfile.csv', index=False, header=False)

Output:

mainfile.csv

Fruit,Animals,Numbers
Fruta,Animales,NĂºmeros
Apple,Cat,5
Banana,Dog,8
Cherry,Goat,2
Durian,Horse,4
Answered By: ScottC

Combine the two files into a new one.

UPDATED to address comment from ScottC.

cat hdr.csv                                                                                                                                                                
first_col,second_col,third_col

cat data.csv                                                                                                                                                              
col1,col2,col3
1,2,3
4,5,6
7,8,9


with open('new_file.csv', 'w') as new_file:
    with open('data.csv') as data_file:
        new_file.write(data_file.readline())
        with open('hdr.csv') as hdr_file:
            new_file.write(hdr_file.readline())
        new_file.write(data_file.read())


cat new_file.csv                                                                                                                                                          
col1,col2,col3
first_col,second_col,third_col
1,2,3
4,5,6
7,8,9

Answered By: Adrian Klaver
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.