Task <<
Previous Next >> Task2
Task1
stage3_ag1|網站|倉儲
40823131|網址:40823131|倉儲:40823131
a40823112|網址:a40823112|倉儲:a40823112
40823123|網址:40823123|倉儲:40823123
40823145|網址:40823145|倉儲:40823145
40823136|網址:40823136|倉儲:40823136
40823109|網址:40823109|倉儲:40823109
40823116|網址:40823116|倉儲:40823116
40823108|網址:40823108|倉儲:40823108
stage3_ag2|網站|倉儲
40823151|網址:40823151|倉儲:40823151
40623121|網址:40623121|倉儲:40623121
40871106|網址:40871106|倉儲:40871106
40823102|網址:40823102|倉儲:40823102
40823104|網址:40823104|倉儲:40823104
40823106|網址:40823106|倉儲:40823106
40823101|網址:40823101|倉儲:40823101
40823132|網址:40823132|倉儲:40823132
stage3_ag3|網站|倉儲
40823119|網址:40823119|倉儲:40823119
40823150|網址:40823150|倉儲:40823150
40823103|網址:40823103|倉儲:40823103
40823107|網址:40823107|倉儲:40823107
40523252|網址:40523252|倉儲:40523252
40823154|網址:40823154|倉儲:40823154
stage3_ag4|網站|倉儲
40823142|網址:40823142|倉儲:40823142
40823144|網址:40823144|倉儲:40823144
40823127|網址:40823127|倉儲:40823127
40823148|網址:40823148|倉儲:40823148
40823121|網址:40823121|倉儲:40823121
40823135|網址:40823135|倉儲:40823135
40823114|網址:40823114|倉儲:40823114
40823146|網址:40823146|倉儲:40823146
stage3_ag5|網站|倉儲
40823111|網址:40823111|倉儲:40823111
40823115|網址:40823115|倉儲:40823115
40823128|網址:40823128|倉儲:40823128
40823120|網址:40823120|倉儲:40823120
40823140|網址:40823140|倉儲:40823140
40823124|網址:40823124|倉儲:40823124
40823139|網址:40823139|倉儲:40823139
40823126|網址:40823126|倉儲:40823126
stage3_ag6|網站|倉儲
40823152|網址:40823152|倉儲:40823152
40823110|網址:40823110|倉儲:40823110
40823122|網址:40823122|倉儲:40823122
40823125|網址:40823125|倉儲:40823125
40823117|網址:40823117|倉儲:40823117
40823129|網址:40823129|倉儲:40823129
40823149|網址:40823149|倉儲:40823149
40823153|網址:40823153|倉儲:40823153
參考40823122修改
讀取 stage3_2a.txt
# open file, default is read mode, since txt content no chinese char4
# no encoding = "UTF-08" is needed
with open("stage3_2a.txt") as fh:
# readlines will read into the whole line and put into list format 23
# has \n at the end of each line 13
data = fh.readlines()
data = [a.replace('4823122','40823122') for a in data]
data = [c.replace('\t\t\t\t','') for c in data]
print(data)
#print(len(data))1
for i in range(len(data)):
group = data[i].rstrip("\n").split("\t")
print('<p>'+group[0]+'|<a href="https://'+group[1]+'.github.io/stage3-ag'+group[0][9]+'">網站</a>|<a href="https://github.com/'+group[2]+'/stage3-ag'+group[0][9]+'">倉儲</a></p>')
# the following will use group data to generate needed html
for j in range(2,18,2):
try:
print('<p>'+group[j]+'|網址:'+'<a href="https://'+group[j]+'.github.io/cd2021'+'">'+group[j]+'</a>'+'|倉儲:'+'<a href="https://github.com/'+group[j]+'/cd2021'+'">'+group[j]+'</a></p>')
except:
continue
以下為每組亂數抽選 2 名組員的程式碼:
# open file, default is read mode, since txt content no chinese char
# no encoding = "UTF-8" is needed
import random
# number of group menber to draw
num = 2
# check if data is "" or not
def notVacant(data):
if data == "":
return False
else:
return True
with open("stage3_2a.txt") as fh:
# readlines will read into the whole line and put into list format
# has \n at the end of each line
data = fh.readlines()
#print(len(data))
# big group list
bgroup = []
# count from the second group member
sgroup = []
for i in range(len(data)):
group = data[i].rstrip("\n").split("\t")
#print(group)
# use mem to count the total number of each group
mem = 0
# final group data
fgroup = []
# count from the second group member, eliminate the first element
sgroup = group[1:]
# get only the odd index number
igroup = [i for i in range(len(sgroup)) if i % 2 == 1]
for j in igroup:
# index starts from 0 which is j-1 when j=1
if notVacant(sgroup[j-1]) == True:
mem += 1
fgroup.append(sgroup[j-1])
print("group " + str(i+1) + ":" + str(mem))
# shuffle the fgroup list
random.shuffle(fgroup)
# draw num of member from final group list: fgroup
for k in range(num):
try:
print(fgroup[k])
except:
# num is greater than total number of this group
print("no such member")
# seperator
print("-"*20)
# the following will use group data to generate needed html
Task <<
Previous Next >> Task2