Hello-

I do not have much experience with python, although I am trying to write a script to do the following:

I have more than one CSV file, in this example I have three, each file has many rows, and two columns, they are in this format:

Column 1 is a time stamp, and Column 2 is data
nn:nn:nn.nnn,nn.n

03:13:25.394,20.4
03:20:03.649,18.3
03:21:34.628,19.9

What I want to be able to do is take two or more files of data all in the same format, and combine them into one file. where the file will be in this format: time,datafromfile1,datafromfile2,datafromfile3...etc

One of the problems i have been having is that each file does not have the same time intervals, so in that case, i imagine there would be some missing data points in each row (which is ok)

I'm not too sure where to start...

Any help is appreciated,
Thank you!

Let me start you off with opening and reading a csv file:

fh = open( 'test.csv', 'r' )
lines = fh.readlines()
fh.close()

for each_line in lines:
    line_data = each_line.strip().split(',')

The open() method allows you to open a file, which I opened in the 'r' mode (for reading). I then read the contents of the file into a list called lines using the readlines() method, which returns a list containing each line as an element.

I then iterated through each line, and performed a strip() to remove and excess whitespace (like the new line character \n), and a split(',') to split the line up at the commas.

At this point you could do whatever you pleased with line_data, like storing it to a list, printing it, etc...

You would use a dictionary as a container. The key would be the time stamp. Each key would have a list that would be initialized to something like ["*", "*", "*"], assuming there aren't any "*"s in the file. The first file would replace the first "*", or zero element, for the time stamp, the second file the second, and ditto for the third. After all files have been read and the dictionary updated (via a function--you don't have to write a separate routine for each file), you print/write it to a file. In the combined file, you will use everything that is not equal to "*".

This article has been dead for over six months. Start a new discussion instead.