with open('data6.csv','r',encoding='gbk') as f:
data1 = f.read().split('\n')
del data1[0]
print('原始数据:')
print(data1)
for i in range(len(data1)):
data1[i] = float(data1[i])
data2 = []
for i in range(len(data1)):
data2.append(int(data1[i]+0.5))
print('四舍五入后数据:')
print(data2)
title = ['原始数据','四舍五入后数据']
info = []
for i in range(len(data1)):
info.append([str(data1[i]),str(data2[i])])
with open('data6_processed.csv','w',encoding='gbk') as f:
f.write(','.join(title)+'\n')
for item in info:
f.write(','.join(item)+'\n')
with open('data7.csv','r',encoding='gbk') as f: data1 = f.read().split('\n') del data1[0] lsta = [] lstm = [] for i in data1: data1.sort(key = lambda i :(i[-2],i[-1]),reverse = True) for i in data1: lst1 = i.split(',') if lst1[2] == 'Acting': lsta.append(lst1) else: lstm.append(lst1) info = lsta+lstm title = ['学号','姓名','专业','分数'] with open('data7_processed.csv','w',encoding='gbk') as f: f.write(','.join(title)+'\n') for items in info: f.write(','.join(items)+'\n') print('{:<10}'.format(title[0]),'{:<10}'.format(title[1]),'{:<10}'.format(title[2]),'{:<15}'.format(title[3])) for i in info: print('{:<10}'.format(i[0]),'{:<10}'.format(i[1]),'{:<10}'.format(i[2]),'{:<15}'.format(i[3]))
def is_valid(a): if len(a) != 18: return False elif (a[:-1].isnumeric() and a[-1] == 'X') or (a.isnumeric()): return True else: return False with open('data9_id.txt','r') as f: data = f.read().split('\n') del data[0] data1 = [] print('姓名,出生日期,年龄') for i in data: lst = i.split(',') data1.append(lst) lst1 = [] import datetime t = datetime.datetime.now().year for i in data1: if is_valid(i[1]) == True: name = i[0] btd = i[1][6:14] age = str(int(t)-int(i[1][6:10])) lst1.append([name,btd,age]) lst1.sort(key=lambda x:x[2],reverse=True) for i in lst1: print(i[0],end=',') print(f'{i[1][:4]}-{i[1][4:6]}-{i[1][6:8]}',end=',') print(i[2])
标签:open,lst1,data,len,实验,print,data1 From: https://www.cnblogs.com/ysxb114514/p/17457085.html