I have a text file 'lines.txt' as shown:
0.02 7 433 649 239 32
0.04 84 43 594 2 321
0.06 65 34 94 5 138
0.08 43 56 23 10 432
1.00 2 382 37 29 102
1.20 573 475 82 757 29
Time (s)
Number (unit)
Third (none)
Fourth (none)
Fifth (none)
Test (none)
The bottom strings are actually the column names of the data above (left to right, so Time_1 is 0.02, Number_1 is 7 etc).
I've already split each of the row strings using .split function. I'm now looking to insert the bottom 6 text strings as column headers for the now separated float strings.
How would I write a function that would separate the two bits of data so I can then assign the bottom strings as column heads?
So far I have:
f = open('lines.txt','r')
lines = f.readlines()
for line in lines:
data = line.split()
data1 = line.strip()
print(data)
for line in data:
if data is not object:
data = [float(x) for x in data]
I think turns the data that appears in numerical form to floats but I cant get it to stop reading when it reaches a text string (Time (s)). Any help is appreciated
If you can't convert the line, save it as a heading:
Output:
To write to a CSV, add:
output.csv: