我有這樣的文件中的內容,我想從中提取只浮點值。該文本文件也包含新行,它也應該在Python中刪除。Python:如何從文本文件中提取浮點值?
hub,0.0166
cord,0.0166
ever,0.0332
switch,0.0498
sleep,0.06639
allow,0.09959
work,0.14939
我已經試過這樣:
newDict = list()
for words in file:
splitline = words.split()
newDict.append("{0}\t{1}".format(splitline[0],log(float(splitline[1]))))
newDict[float(splitline[0])] = ",".join(splitline[1:])
print(newDict)
我觀察到的錯誤是:
Traceback (most recent call last):
File "G:\Setups\Python\chi-1.py", line 11, in <module>
newDict.append("{0}\t{1}".format(splitline[0],log(float(splitline[1]))))
IndexError: list index out of range
但我並沒有有任何輸出這表明我的錯誤。循環中的變量文件包含文本文件。因此,如果有人知道如何只提取浮點數。善意幫助它。
由於提前
什麼錯誤 – Surajano
你忘了分隔符:'分裂線= words.split( '')' –
將它顯示錯誤--- '回溯(最近通話最後一個): 文件「G:\設置\ Python \ chi-1.py「第17行 newDict [float(splitline [0])] =」,「。join(splitline [1:]) ValueError:無法將字符串轉換爲float:' –