看板 Python 關於我們 聯絡資訊
是這樣的,我存在txt檔案中的內容如下 0.84 0.0376359 0.85 0.0304838 0.86 0.0236949 0.87 0.0174301 0.88 0.0081633 0.89 0 0.9 0 0.91 0 0.92 0 0.93 0 0.94 0 0.95 0 0.96 0 0.97 0 0.98 0 0.99 0 1 0 之前處理的檔案並沒有0和1,都是小數 而那時我是這樣讀檔案的 f=open("fi2.dat",'r') for i in range(n): ct=f.readline() a=ct.strip().split('\t') dz[i] =string.atof(a[0]) dd[i] =string.atof(a[1]) f.close 這種做法在之前的檔案還可以用,但如今換只要檔案中出現了整數就會出現錯誤訊息 想請問各位版友們有沒有解決的辦法? -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.113.47.69
Enzo1: 未看先猜1樓是帥哥 2樓 KI780804 3樓ZSH07/15 01:37
KI780804:樓上Enzo1樓下zsh07/15 01:37
zsh:劣退 BY ZSH07/15 01:37
-- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 114.34.188.129
lc85301:float(a[0])呢? 08/11 20:54
z101924512:能po一下錯誤訊息嗎? 08/11 20:59
Traceback (most recent call last): File "./fea.py", line 33, in <module> z[i] =string.atof(a[0]) File "/usr/lib64/python2.7/string.py", line 388, in atof return _float(s) ValueError: could not convert string to float: 麻煩大家了... ※ 編輯: LeeGarDer 來自: 114.34.188.129 (08/11 21:28)
jlhg:讀到空字串 08/11 21:48
已解決......不是讀檔問題 是我後面不小心有個元素除到0了..... ※ 編輯: LeeGarDer 來自: 114.34.188.129 (08/11 22:55)