[問題] 將list快速寫入檔案的方式?

看板Python作者 (5566520)時間9年前 (2016/03/02 22:43), 編輯推噓1(101)
留言2則, 2人參與, 最新討論串1/3 (看更多)
f = open('123.txt','r') lines = f.readlines() print len(lines) #2000000 lines startindex = 30 endindex = 15000 outputfile = open('456.txt','w') for i in range(startindex,endindex): outputfile.write(lines[i]) outputfile.close() 想詢問除了這樣寫以外 有無更快的寫法 可以直接把string list寫到檔案內的寫法 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 123.195.222.114 ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1456929799.A.310.html

03/02 23:36, , 1F
不要用readlines;改用islice吧
03/02 23:36, 1F

03/03 13:26, , 2F
可以用看看cStringIO, 概念像是pseudo file.
03/03 13:26, 2F
文章代碼(AID): #1Mrlm7CG (Python)
文章代碼(AID): #1Mrlm7CG (Python)