实验六 文件应用编程-2

互联网 2022/5/22 9:34:37

#test-3.py def is_valid(m):if len(m)==18 and m.isdigit():return 合法elif len(m)==18 and m[-1]==X:return 合法else:return 不合法with open(data3_id.txt,r,encoding = utf-8) as f:data = f.readlines() list = [] for i in data:data_ = i.strip(\n)if is_valid(d…
#test-3.py
def is_valid(m):
    if len(m)==18 and m.isdigit():
        return '合法'
    elif len(m)==18 and m[-1]=='X':
        return '合法'
    else:
        return '不合法'

with open('data3_id.txt','r',encoding = 'utf-8') as f:
    data = f.readlines()
list = []
for i in data:
    data_ = i.strip('\n')
    if is_valid(data_[3:]) == '合法':
        list.append(data_)
list.sort(key=lambda x: x[9:13])
for m in list:
    print('{},{}-{}-{}'.format(m[:2],m[9:13],m[13:15],m[15:17]))

#tesk5_1.py
import random
import datetime
t = datetime.datetime.now()
m = eval(input('输入随机抽点人数:'))
with open('data5.txt', encoding = 'utf-8') as f:
    data = f.readlines()
list = []
for i in range(m):
    x = random.choice(data)
    list.append(x)
    data.remove(x)
filename = t.strftime('%Y%m%d') + '.txt'
with open(filename, "w+", encoding="utf-8") as f:
    f.writelines(list)
    f.seek(0)
    print(f.read())

#tesk5-2.py
import random
import datetime
t = datetime.datetime.now()
print("{0:{1}{3}{2}}".format("抽点开始", "=", 40, "^"))
list_ = []
while True:
    n = eval(input("输入随机抽点人数: "))
    if n == 0:
        break
    else:
        with open("data5.txt", encoding="utf-8") as f:
            data = f.readlines()
        list = []
        for i in range(n):
            x = random.choice(data)
            list.append(x)
            data.remove(x)
        with open("20220517.txt", "w+", encoding="utf-8") as f:
            f.writelines(list)
            f.seek(0)
            print(f.read())
    list_ += list
filename = t.strftime('%Y%m%d') + '.txt'
with open(filename, "w", encoding="utf-8") as f:
    f.writelines(list_)
print("{0:{1}{3}{2}}".format("抽点结束", "=", 40, "^"))

 

------------恢复内容开始------------

------------恢复内容开始------------

------------恢复内容结束------------

------------恢复内容结束------------

随时随地学软件编程-关注百度小程序和微信小程序
关于找一找教程网

本站文章仅代表作者观点,不代表本站立场,所有文章非营利性免费分享。
本站提供了软件编程、网站开发技术、服务器运维、人工智能等等IT技术文章,希望广大程序员努力学习,让我们用科技改变世界。
[实验六 文件应用编程-2]http://www.zyiz.net/tech/detail-324275.html

赞(0)
关注微信小程序
程序员编程王-随时随地学编程

扫描二维码或查找【程序员编程王】

可以随时随地学编程啦!

技术文章导航 更多>