[問題] np.loadtxt會少項

看板Python作者 (karco)時間7年前 (2018/03/16 14:26), 編輯推噓1(102)
留言3則, 1人參與, 7年前最新討論串1/1
大大好 小弟正在製作一支程式 能夠隨機產生n列2行的矩陣 成功生成x.txt後 發現之後如果想要讀取 利用np.loadtxt載入 載入後的列數會比原本的txt檔還少 請問我哪邊犯了錯嗎? 舉例子 如下面程式碼會有10000列2行 載入後 大概會只剩下97xx列2行 請問有解決的方法嗎? 可以發現重新輸出的out.txt範圍變小了@@ 程式碼如下: from random import randint import numpy as np a = 0 c = 0 d = 0 numbers = 10000 f = open ('x.txt', 'w') while a < numbers: c = float(randint(1, 90)) d = float(randint(1, 90)) e = str(c) g = str(d) f.write(e +' ' + g+ '\n') a = a+1 f.close b = np.loadtxt('x.txt').astype(np.float32) np.savetxt('out.txt',b) -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 140.113.121.149 ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1521181589.A.253.html

03/16 16:57, 7年前 , 1F
f.close(),括號沒寫啦....然後,numpy 就有random可用了...
03/16 16:57, 1F

03/16 16:58, 7年前 , 2F
loadtxt本身就可以指定dtype了.....
03/16 16:58, 2F

03/16 16:59, 7年前 , 3F
認真點查個API吧...
03/16 16:59, 3F
文章代碼(AID): #1QgsEL9J (Python)
文章代碼(AID): #1QgsEL9J (Python)