我無法處理將字符串列表拆分爲浮點列表的問題。我打開一個文本文件(.vol,但它只是包含文本),最後一行是非常長的一行數字。將字符串列表拆分爲浮點數 - 內存錯誤
Params1 437288.59375000 102574.20312500 -83.30001831
Params2 437871.93750000 104981.40625000 362.10000610
Params3 0.00000000
Params4 342 1416 262
Params5 583.34375000 2407.19995117 445.40002441
Params6 20.00000000
Params7 1.70000005
Params8 126879264個
值:
0.25564435 0。 462439 0.1365 0.1367 26.00000000(等等,還有數以百萬計值)
因爲它是一個txt文件的10日線,我將它們加載到一個列表:
with open('d:/py/LAS21_test.vol') as f:
txt = []
for line in f:
txt.append(line)
然後我試着轉換從字符串彩車通過:
A = []
for vx in txt[9]:
try:
A.append(float(vx))
except ValueError:
pass
print (A[0:20])
print (txt[9][0:20])
這給我的結果:
[0.0, 2.0, 5.0, 5.0, 6.0, 4.0, 4.0, 3.0, 5.0, 0.0, 4.0, 6.0, 2.0, 4.0, 3.0, 9.0, 0.0, 1.0, 3.0, 6.0]
0.25564435 0.462439
我想有是正確拆分花車的列表,如:
[0.25564435, 0.462439]
我以前except ValueError
忽略空格 - 當只用float(txt[9])
我得到的值錯誤。 第二個問題:我不能使用txt[9].split
,因爲那時我得到'內存錯誤'。
我該如何將其轉換爲正確的浮動列表?
如果要在添加的輸入文件的內容更容易理解。 – AntoineB
剛剛添加,謝謝。 –