How to convert a split string into floats but ignore objects in Python?

44 views Asked by At

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

3

There are 3 answers

0
Mark Tolonen On BEST ANSWER

If you can't convert the line, save it as a heading:

with open('lines.txt') as file:
    data = []
    headings = []
    for line in file:
        try:
            data.append([float(x) for x in line.split()])
        except ValueError:
            headings.append(line.strip())
print(headings)
for line in data:
    print(line)

Output:

['Time (s)', 'Number (unit)', 'Third (none)', 'Fourth (none)', 'Fifth (none)', 'Test (none)']
[0.02, 7.0, 433.0, 649.0, 239.0, 32.0]
[0.04, 84.0, 43.0, 594.0, 2.0, 321.0]
[0.06, 65.0, 34.0, 94.0, 5.0, 138.0]
[0.08, 43.0, 56.0, 23.0, 10.0, 432.0]
[1.0, 2.0, 382.0, 37.0, 29.0, 102.0]
[1.2, 573.0, 475.0, 82.0, 757.0, 29.0]

To write to a CSV, add:

with open('output.csv', 'w', newline='') as file:
    writer = csv.writer(file)
    writer.writerow(headings)
    writer.writerows(data)

output.csv:

Time (s),Number (unit),Third (none),Fourth (none),Fifth (none),Test (none)
0.02,7.0,433.0,649.0,239.0,32.0
0.04,84.0,43.0,594.0,2.0,321.0
0.06,65.0,34.0,94.0,5.0,138.0
0.08,43.0,56.0,23.0,10.0,432.0
1.0,2.0,382.0,37.0,29.0,102.0
1.2,573.0,475.0,82.0,757.0,29.0
0
Sujay S Badge On

hoping you need an array with line 1 as heading and rest as rows

f = open('try.txt','r')
lines = f.readlines()
data={}
cols=[]

# checking each lines and converting all the data to float which is not colomun heading
for index,line in enumerate(lines):
    data[index]=[]
    for col in line.split():
      try:
        data[index].append(float(col))
      except ValueError:
         #if data is not convertable to float add the main line data to cols array
         cols.append(line)
         
         break
finalarray = []
for key,value in data.items():
   if len(value)==0:
      #ignoring the empty dict which represents line for col
      continue
   else:
      #appending lists of float to final array
      finalarray.append(value)
#inserting col headings to the index zero
finalarray.insert(0,cols)
#printing final array where line one is col heading and all other lines is rowdata
print(finalarray)
0
Sowmya On

If you know that the bottom 6 lines are always going to be your column names, then you can split the list into two.

f = open('lines.txt','r')
lines = f.readlines()
columns = lines[-6:]
lines = lines[:-6]
for line in lines:
    data = line.split()
    data1 = line.strip()
    print(data)...

Use the columns to assign your column name