Reputation: 1040
I have a data set in .csv
files.
They are each two dimensional ( multiple columns and multiple rows ).
The third dimension being the depth of sixty or so files, labeled according to date.
I need to use these as one data set and don't know where to start.
Is there a tool set that handles this effectively?
I believe that my essential question is: Is there a way to add the file name of a .csv file to the contents of the file and combine multiple .csv files together?
Upvotes: 0
Views: 333
Reputation: 7293
If "file1.csv" contains
a,1
b,2
and "file2.csv" contains
aa,11
bb,22
you can put them together with something like
out = open("outfile.csv", "w")
for fname in ["file1.csv","file2.csv"]:
with open(fname) as f:
for line in f:
out.write(line.rstrip() + ',' + fname + '\n')
out.close()
and get
a,1,file1.csv
b,2,file1.csv
aa,11,file2,csv
bb,22,file2.csv
You may also be interested in modules os
(to remove the extension or a path before the name, for example) and maybe fileinput
from the standard library.
Depending on the data it might also be better to construct a database (with sqlite3
) instead.
Upvotes: 1