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

实验6

作者:互联网

task3
# Created on 刘杰的iPad.
f1=open('data3_id.txt','r')
list1,list3=[],[]
for i in f1.readlines():
    list1.append(i.strip().split(','))
def is_valid(x):
    if len(x)==18:
        for i in x:
            if'1'<=i<='9'or i=='X':
                return True
            else:
                return False
    else:
        return False
list2=list1[0];del list1[0]
print(','.join(list2))
for i in list1:
    y=i[1]
    if is_valid(y)==True:
        z=i[1][6:10]+'-'+i[1][11:12]+'-'+i[1][13:14]
        print(i[0]+','+z)
f1.close

 

 task5_1

import random

n = int(input('输入随机抽点人数:'))

with open('实验6/data5.txt', 'r', encoding='utf-8')as f:
    data = random.sample(f.readlines(), n)

import datetime

t = datetime.date.today()
filename = t.strftime('%Y%m%d') + '.txt'

with open(f'实验6/{filename}', 'w+', encoding='utf-8')as f:
    f.writelines(''.join(data).rstrip('\n'))
    f.seek(0)
    print(''.join(f.readlines()))

 

 task5_2

# Created on 刘杰的iPad.
f1=open('data3_id.txt','r')
list1,list3=[],[]
for i in f1.readlines():
    list1.append(i.strip().split(','))
def is_valid(x):
    if len(x)==18:
        for i in x:
            if'1'<=i<='9'or i=='X':
                return True
            else:
                return False
    else:
        return False
list2=list1[0];del list1[0]
print(','.join(list2))
for i in list1:
    y=i[1]
    if is_valid(y)==True:
        z=i[1][6:10]+'-'+i[1][11:12]+'-'+i[1][13:14]
        print(i[0]+','+z)
f1.close

 

标签:f1,list1,readlines,实验,刘杰,txt,open
来源: https://www.cnblogs.com/SAKURA-QiQi/p/16307932.html