I'm trying to write a bit of code that reads in a dat file that has a bunch of blank lines and put them into lists to be manipulated later.
Ex:
0.92; 0.70
1.53;
1.41; 1.00
1.47; 1.08
;
0.73; 0.18
1.50; 1.17
;
;
1.68;
I would like to skip the lines that have blank spaces.
This is what I have so far...
file_name2 = 'Gliese.dat'
filein2 = open(file_name2, 'r')
lines2 = filein2.readlines()
data2 = []
filein2.close()
for line in lines2:
data2.append(line)
for i in data2:
items2 = i.split(';')
if items2[0]=='' or items2[1]=='':
items2.strip()
else:
ms_U_B.append(float(items2[1]))
ms_B_V.append(float(items2[0]))
I'm getting the error "ValueError: could not convert string to float:' ' ".
Rather new to python and would appreciate any help :)
You get the error cause you try to convert the empty spaces to float. Try this:
with open("Gliese.dat", "r")as f:
data = []
for line in f:
line = line.strip()
if line.split(";")[0] and line.split(";")[1]:
data.append(line.rstrip("\n"))