其他分享
首页 > 其他分享> > 实验六

实验六

作者:互联网

#task3.py
def is_valid(x):
    if len(x) != 18:
        return False
    else:
        for i in x:
            try:
                i = int(i)
            except:
                if ord(i) != ord('X'):
                    return False
        return True

with open('data3_id.txt', 'r', encoding='utf-8')as f:
    data = f.readlines()

data1 = [data[i].rstrip('\n').split(',')for i in range(1,len(data))]

data2 = []
for i in data1:

    if is_valid(i[1]) == True:
        data2.append(i)

for i in data2:
    i[1] = f'{i[1][6:10]}-{i[1][10:12]}-{i[1][12:14]}'

data2.sort(key=lambda ele:ele[1])

for i in data2:
    print(','.join(i))

 

#task5_1.py
import random

n = int(input("输入随机抽点人数: "))
with open("data5.txt", encoding="utf-8") as f:
    data = f.readlines()
data_ = data.copy()
lucky_people = []
for i in range(n):
    x = random.choice(data_)
    lucky_people.append(x)
    data_.remove(x)
with open("20220517.txt", "w+", encoding="utf-8") as f:
    f.writelines(lucky_people)
    f.seek(0)
    print(f.read())

 

 

 

 

#task5_2.py
import random

print("{0:{1}{3}{2}}".format("抽点开始", "=", 40, "^"))
active_input = True
ls = []
while active_input:
    n = int(input("输入随机抽点人数: "))
    if n == 0:
        break
    else:
        with open("data5.txt", encoding="utf-8") as f:
            data = f.readlines()
        data_ = data.copy()
        lucky_people = []
        for i in range(n):
            x = random.choice(data_)
            lucky_people.append(x)
            data_.remove(x)
        with open("20220517.txt", "w+", encoding="utf-8") as f:
            f.writelines(lucky_people)
            f.seek(0)
            print(f.read())
    ls += lucky_people
with open("20220517.txt", "w", encoding="utf-8") as f:
    f.writelines(ls)
print("{0:{1}{3}{2}}".format("抽点结束", "=", 40, "^"))

 

标签:encoding,open,people,lucky,实验,txt,data
来源: https://www.cnblogs.com/bamboosama/p/16304768.html