Search code examples
pythoncsvappendrow

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


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


Solution

  • 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