python实现简单的抽奖小程序,抽奖的内容从文件里面读取

打开文件,读取文件的内容,随机抽出一个然后删掉已经抽出来的号码

 

import random
from random import choice
f = open('allnum.txt')
data = f.read()
a = data
b = a.strip('\n').split('\n')
print(b)
y = choice(b)
print(y)
lines = [l for l in open("allnum.txt", "r") if l.find(y, 0) != 0]
fd = open("allnum.txt", "w")
fd.writelines(lines)
fd.close()
f.close

 

txt文件内容如下:

28002258
28002259
28002260
28002261
28002262
28002263
28002264
28002265
28002266
28002267
28002268
28002269
28002270
28002271
28002272
28002273
28002274

posted @ 2019-01-02 16:07  pennychenpei  阅读(774)  评论(0编辑  收藏  举报