Giovanni Zambotti
Giovanni Zambotti

Reputation: 1

how add new line in CSV file using python

I have a csv file that contains time (hh:mm:ss) and date (mm/dd/yyyy) fields, and sixteen more fields.

Generally the time field display time every 30'', but sometime the separation might be more (minutes or even hours).

For instance:

1/27/2011 12:10:00 
1/27/2011 12:10:30 
1/27/2011 12:11:00 
1/27/2011 12:15:00

I need to add new lines (as many as the gap between them) every time the gap between a line and the next is more the 30'', and fill them out with the values from the first line in the gap.

I would like to do it without working in a database environment. Is it possible? If so, can you give me some good tips?

Upvotes: 0

Views: 1415

Answers (3)

Peter
Peter

Reputation: 61

I'm not sure I understand your situation. If this is how the file is set up:

1/27/2011 12:10:00 xxx xxx xxx xx...
1/27/2011 12:10:30 xxx xxx xxx xx...
1/27/2011 12:10:00 xxx xxx xxx xx...
1/27/2011 12:15:00 xxx xxx xxx xx...

then you can read the file (or maybe just the last two lines) into your program, maybe as a list of lines, check to see if the time difference between the two lines is > 30", and if need be insert two lines into the array and rewrite the whole file. Or, instead of that, you could try to edit the file in place.

Upvotes: 0

endre
endre

Reputation: 1373

Have you checked the manual? http://docs.python.org/library/csv.html there are also examples at the end

The first idea that comes to my mind is iterating over the rows in the csv and copying them to a new blank csv one row at a time, if current row and row before are more than 30" apart, then add row before the necessary amount of times to the new csv.

Upvotes: 0

Alex Reynolds
Alex Reynolds

Reputation: 96927

Buffer the last two lines, calculate the time difference between them, and write output based upon the results of a conditional test on that time difference.

Upvotes: 1

Related Questions