6.实验任务6
程序源代码:
with open('data6.csv','r',encoding = 'gbk') as f: data = f.readlines() data_title = ['原始数据','四舍五入后数据'] data.remove('原始数据\n') data_zahl = [] for i in data: if i[-1]=='\n': i = i[:-1] data_zahl.append(float(i)) data_new = [int(i+0.5) for i in data_zahl] with open('data6_processed.csv','w',encoding= 'gbk') as f: f.write(','.join(data_title) + '\n') for i in range(len(data_zahl)): f.write(str(data_zahl[i])+','+str(data_new[i])+'\n') print('原始数据:') print(data_zahl) print('四舍五入后数据:') print(data_new)
运行程序截图:
7.实验任务7
程序源代码:
with open('data7.csv','r',encoding = 'gbk') as f: data = f.readlines() data_t = ['学号','姓名','专业','分数'] data1 = [i.strip('\n').split(',') for i in data] data1.remove(data1[0]) data2=sorted(data1,key=lambda x:(x[2],-int(x[3]))) with open('data7_processed.csv','w',encoding = 'gbk') as f: f.write(','.join(data_t) + '\n') for item in data2: f.write(','.join(item) + '\n') print(f'{"学号":10s}{"姓名":10s}{"专业":10s}{"分数":10s}') for a,b,c,d in data2: print('{:<10s}{:<10s}{:<10s}{:<10}'.format(a,b,c,d))
运行程序截图:
8.实验任务8
程序源代码:
with open('hamlet.txt','r',encoding='utf-8')as f: data = f.read() data1 = f.readlines() print('hamlet.txt粗略统计:') print(f'行数:{len(data.splitlines())}') print(f'单词数:{len(data.split())}') print(f'字符数:{len(data)}') print(f'空格数:{data.count(" ")}') data2 = [] for i in range(len(data1)): data2.append('{}'.format(i+1)+'.'+data1[i]+'\n') with open('hamlet_with_line_number.txt','w',encoding='utf-8') as f: for i in data2: f.write(','.join(data2)+'\n')
运行程序截图:
9.实验任务9
程序源代码:
def is_valid(n): ls = [] for i in range(10): ls.append(str(i)) t = str(n) if len(t)!=18: return False else: i = 0 while i <17: if t[i] not in ls: return False break else: i+=1 else: if t[17] in ls or t[17]=='X': return True else: return False with open('data9_id.txt','r',encoding = 'utf-8') as f: data = f.readlines() data.remove('姓名,身份证号码\n') data1 = [i.strip('\n').split(',') for i in data] for i in data1: if is_valid(i[1]): continue else: data1.remove(i) data2=sorted(data1,key=lambda x:(x[1][6:13])) for i in data2: nl = 2023 - int(i[1][6:10]) i.append(nl) print('姓名,出生日期,年龄') for a,b,c in data2: print('{},{},{}'.format(a,b,c))
运行程序截图:
10.实验任务10
task10-1.py
- 程序源代码:
with open('data10_stu.txt','r',encoding = 'utf-8') as f: data = f.readlines() data1 = [i.strip('\n').split(',') for i in data] print('{:*^40}'.format('抽点开始')) n = int(input('输入随机抽点人数:')) import random x = list(range(len(data1))) t = random.sample(x,n) for i in t: for j in data1[i]: print(j) with open('20230605.txt','w',encoding='utf-8') as f: for i in t: f.write(','.join(data1[i])+'\n')
运行程序截图:
task10-2.py
程序源代码:
with open('data10_stu.txt','r',encoding = 'utf-8') as f: data = f.readlines() data1 = [i.strip('\n').split(',') for i in data] print('{:*^40}'.format('抽点开始')) n = int(input('输入随机抽点人数:')) d= [] while n!=0: import random x = list(range(len(data1))) t = random.sample(x,n) for i in t: d.append(data[i]) for j in data1[i]: print(j) n = int(input('输入随机抽点人数:')) else: print('{:*^40}'.format('抽点结束')) with open('20230605.txt','w',encoding='utf-8') as f: for i in d: f.write(''.join(i)+'\n')
运行程序截图:
标签:open,encoding,data1,len,实验,print,data From: https://www.cnblogs.com/nettj666/p/17458442.html