1

I have a folder of around 200 CSV files. Each only have values in the first row. How do I combine the files so that is each CSV file becomes a row in the new dataset.

For example:

1.csv contains:
[1, 2, 3, 4, 5, 6]
2.csv contains:
[2, 3, 4, 5, 5, 7]

I want to combine the CSV's so that the final CSV looks like:

final.csv contains:
[1, 2, 3, 4, 5, 6]
[2, 3, 4, 5, 5, 6]

*Each matrix is a row of the .csv, and each , means a new cell.

Thank You!

2
  • 1
    Matrix? I don't understand, do your csv's actually contain "[1, 2, .. that is, do they actually have those brackets? Commented Jun 15, 2017 at 18:16
  • 1
    Honestly, you could do this with just cat Commented Jun 15, 2017 at 18:16

2 Answers 2

4

If you have bash, you can use

cat *.csv > combined.csv

Or, if you want to do it the python way:

import csv

with open('combined.csv', 'wb') as out_file:
    writer = csv.writer(out_file)
    for fname in os.listdir('.'):
        with open(fname, 'rb') as in_file:
            for row in csv.reader(in_file):
                writer.writerow(row)

Here you'll need to navigate to the directory containing your 200 odd csv files.

Sign up to request clarification or add additional context in comments.

Comments

0
  • windows cmd - type copy *.csv combined.csv

  • linux bash - cat file1.csv file2.csv > combined.csv

  • python - something like

    fout=open("out.csv","a")
    for num in range(1,201):
        for line in open("sh"+str(num)+".csv"):
             fout.write(line)    
    fout.close()
    

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.