[問題] 讀取txt中的數據

看板Python作者 (Legendre)時間10年前 (2013/08/11 20:03), 編輯推噓3(300)
留言3則, 3人參與, 最新討論串1/1
是這樣的,我存在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

07/15 01:37,
未看先猜1樓是帥哥 2樓 KI780804 3樓ZSH
07/15 01:37

07/15 01:37,
樓上Enzo1樓下zsh
07/15 01:37

07/15 01:37,
劣退 BY ZSH
07/15 01:37
-- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 114.34.188.129

08/11 20:54, , 1F
float(a[0])呢?
08/11 20:54, 1F

08/11 20:59, , 2F
能po一下錯誤訊息嗎?
08/11 20:59, 2F
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)

08/11 21:48, , 3F
讀到空字串
08/11 21:48, 3F
已解決......不是讀檔問題 是我後面不小心有個元素除到0了..... ※ 編輯: LeeGarDer 來自: 114.34.188.129 (08/11 22:55)
文章代碼(AID): #1I1tsU09 (Python)