當前位置:編程學習大全網 - 電腦編程 - python問題請教各位大神:希望實現壹個大txt文件隨機讀取其中幾句話並合成壹篇新文章,謝謝

python問題請教各位大神:希望實現壹個大txt文件隨機讀取其中幾句話並合成壹篇新文章,謝謝

#?-*-?coding:?utf-8?-*-

import?random

with?open('a.txt','r')?as?f:

with?open('b.txt','w')?as?f2:

lines=f.readlines()

flen=len(lines)-1

for?i?in?range(1,random.randint(0,flen)):

f2.write(lines[random.randint(0,flen)])

  • 上一篇:Free Pascal IDE 的編程: “打印出1~20的平方數表。”的程序怎樣編? 請打出!
  • 下一篇:ug模擬時坐標怎樣顯示三位
  • copyright 2024編程學習大全網