0

I want to append two .csv files with the same number/names of columns and save the ouput file. However I don't want to use pandas concat nor pandas read_csv. Is there a way to achieve this using pure python 'with open'?

  • 1
    Check out this similar [question](https://stackoverflow.com/questions/2363731/append-new-row-to-old-csv-file-python). They are just trying to append data, but you can open the second file within the loop and append it to the first. – WVJoe Jan 22 '21 at 16:55

1 Answers1

0

Something like this should work:

# read first file
with open('file1.csv', 'r') as txt:
    lines = txt.readlines()

# extend the list with the lines from the second file
with open('file2.csv', 'r') as txt:
    lines.extend(txt.readlines())

# remove \n from the end of each line
lines = [line.strip() for line in lines]
Jeff
  • 1
  • 3