I am trying to make a program that takes a large data file of integers and creates a new csv in a different format, where it takes the x,y,z
of 30 lines and merges them into one line.
The large dataset is formatted in (timestamp, x,y,z
)
Ex:
0.000, 5, 6, 8,
1.000, -6, 7, 9,
2.000, -15, 25, 23,
or:
timestamp, x1, y1, z1
timestamp, x2, y2, z2
timestamp, x3, y3, z3
The new dataset would look like this:
delta timestamp, x1, y1, z1, x2, y2, z2, x3, y3, z3....x30, y30, z30,
delta timestamp, x31, y31, z31, x32, y32, z32, x33,... x60, y60, z60,
etc.. (each line containing 30 x,y,z
's)
I thought of maybe appending a \n every 30 lines and then replacing each new line with a comma. And my code below doesn't work. It just puts an extra comma to where the new data looks like this:
timestamp, x1, y1, z1,, timestamp, x2, y2, z2,, timestamp...
Do you guys have any ideas?
list = []
import csv
i=0
results = []
with open('bikefall.csv', newline='') as inputfile:
for row in csv.reader(inputfile):
i+=1
if i%30==0:
results.append(row)
results.append('\n')
else:
results.append(row)
print("\n".join([item.replace('\n', ',') for item in
open('bikefall.csv').read().split('\n\n')]))