-
-
Save zilongshanren/5219868 to your computer and use it in GitHub Desktop.
《Cocos2D权威指南》送书脚本
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import csv | |
from random import randint | |
import sys | |
#determin whether args is legal or not | |
#抽奖文件名 | |
filename = sys.argv[1] | |
#中奖序列号起始值 | |
startNum = int(sys.argv[2]) | |
#中奖序列号结束值 | |
endNum = int(sys.argv[3]) | |
#总共多少人中奖 | |
totoalNum = int(sys.argv[4]) | |
if (endNum - startNum) < totoalNum: | |
print 'Invalid args' | |
sys.exit(1) | |
def unicode_csv_reader(utf8_data, dialect=csv.excel, **kwargs): | |
csv_reader = csv.reader(utf8_data, dialect=dialect, **kwargs) | |
for row in csv_reader: | |
yield [unicode(cell, 'gbk') for cell in row] | |
data = unicode_csv_reader(open(filename)) | |
running = True | |
s = set() | |
while running: | |
luckyDog = randint(int(startNum), int(endNum)) | |
if (luckyDog not in s): | |
s.add(str(luckyDog)) | |
if len(s) > 19: | |
break | |
rownum = 0 | |
winnerId = "" | |
for row in data: | |
if rownum == 0: | |
header = row | |
else: | |
column = 0 | |
flag = False | |
for col in row: | |
if col.isdigit() and (col in s): | |
flag = True | |
winnerId = col | |
column += 1 | |
if flag: | |
print '%s:%-8s:%s' % (winnerId, "Winner:", col) | |
rownum += 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
使用样例: python winner.py users.csv 18 475 20
第一个参数是文件名,第2,3个参数是抽奖的始终序号号,最后一个参数是中奖人个数。