[英]How can I make a 2-D Array in Python from a .txt file?
我必須將具有9列和807022行的.txt文件導入程序,以便對其進行排序。 我已經嘗試了代碼:
with open('ExampleTable.txt') as file:
array2d = [[float(digit) for digit in line.split()] for line in file]
f = Find_StDev(Find12EpochStars(array2d), array2d)
print (f)
但是,我收到錯誤消息:ValueError:無法將字符串轉換為float:'%'
文本文件是浮點數和整數。
如何導入.txt文件,以便函數可以使用它?
如果您以后要注意這一點,可以添加快速檢查以確保首先可以進行轉換。
對於數百萬個數字,如果一個值損壞,則不應過多改變答案!
with open('ExampleTable.txt') as file:
array2d = [[float(digit) for digit in line.split() if digit.isnumeric() else 0]
for line in file]
f = Find_StDev(Find12EpochStars(array2d), array2d)
print (f)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.