task6
1 import csv 2 title = [] 3 info = [] 4 5 with open('data6.csv','r',encoding='gbk') as f: 6 f_reader = csv.reader(f) 7 for line in f_reader: 8 info.append(line) 9 x = info.pop(0) 10 title.append(x) 11 title[0].append('四舍五入后数据') 12 for i in info: 13 mypoia = int(float(i[0])+0.5) 14 i.append(str(mypoia)) 15 16 with open('data6_processed.csv','w',encoding='gbk',newline = '') as f: 17 f_writer = csv.writer(f) 18 f_writer.writerow(title[0]) 19 f_writer.writerows(info) 20 21 print(title[0][0] + ':') 22 o = [] 23 for i in info: 24 o.append(i[0]) 25 print(o) 26 print(title[0][1] + ':') 27 p = [] 28 for i in info: 29 p.append(i[1]) 30 print(p)import csv 31 title = [] 32 info = [] 33 34 with open('data6.csv','r',encoding='gbk') as f: 35 f_reader = csv.reader(f) 36 for line in f_reader: 37 info.append(line) 38 x = info.pop(0) 39 title.append(x) 40 title[0].append('四舍五入后数据') 41 for i in info: 42 mypoia = int(float(i[0])+0.5) 43 i.append(str(mypoia)) 44 45 with open('data6_processed.csv','w',encoding='gbk',newline = '') as f: 46 f_writer = csv.writer(f) 47 f_writer.writerow(title[0]) 48 f_writer.writerows(info) 49 50 print(title[0][0] + ':') 51 o = [] 52 for i in info: 53 o.append(i[0]) 54 print(o) 55 print(title[0][1] + ':') 56 p = [] 57 for i in info: 58 p.append(i[1]) 59 print(p)
运行截图:
实验源码:
1 with open('data7.csv','r',encoding='gbk') as f: 2 data1 = f.read().split('\n') 3 del data1[0] 4 lsta = [] 5 lstm = [] 6 for i in data1: 7 lst1 = i.split(',') 8 if lst1[2] == 'Acting': 9 lsta.append(lst1) 10 else:lstm.append(lst1) 11 lstm.sort(key=lambda x:x[-1],reverse = True) 12 lsta.sort(key=lambda x:x[-1],reverse = True) 13 info = lsta + lstm 14 title = ['学号','姓名','专业','分数'] 15 with open('data7_processed.csv','w',encoding='gbk') as f: 16 f.write(','.join(title)+'\n') 17 for items in info: 18 f.write(','.join(items)+'\n') 19 print('{:<10}'.format(title[0]),'{:<10}'.format(title[1]),'{:<10}'.format(title[2]),'{:<15}'.format(title[3])) 20 for i in info: 21 print('{:<10}'.format(i[0]),'{:<10}'.format(i[1]),'{:<10}'.format(i[2]),'{:<15}'.format(i[3]))
运行截图:
实验任务八:
实验源码:
1 lines = 0 2 words = 0 3 sum1 = 0 4 space = 0 5 with open('hamlet.txt','r',encoding='utf-8') as f: 6 for line in f: 7 word = line.split() 8 lines += 1 9 words += len(word) 10 sum1 += len(line) 11 for i in line: 12 if i == ' ': 13 space += 1 14 else: 15 pass 16 print('hamlet.txt粗滤统计:') 17 print(f'行数:{lines}') 18 print(f'单词数:{words}') 19 print(f'字符数:{sum1}') 20 print(f'空格数:{space}') 21 22 with open('hamlet.txt','r',encoding = 'utf-8') as f: 23 text = f.readlines() 24 25 for i in range(len(text)): 26 text[i] = str(i+1) + ' ' + text[i] 27 28 with open('hamlet_with_line_number.txt','w',encoding = 'utf-8') as f: 29 f.writelines(text)
运行截图:
1 def is_valid(sfz): 2 if len(sfz) != 18: 3 return False 4 elif (sfz[:-1].isnumeric() and sfz[-1] == 'X') or sfz.isnumeric(): 5 return True 6 else:return False 7 8 9 with open('data9_id.txt','r',encoding='utf-8') as f: 10 data = f.read().split('\n') 11 del data[0] 12 data2 = [] 13 data3 =[] 14 print('姓名,出生日期,年龄') 15 for i in data: 16 lst = i.split(',') 17 data2.append(lst) 18 lst2 = [] 19 for i in data2: 20 if is_valid(i[1]) == True: 21 name = i[0] 22 btd = i[1][6:14] 23 age = str(2023 - int(i[1][6:10])) 24 lst2.append([name,btd,age]) 25 lst2.sort(key=lambda x:x[2],reverse=True) 26 for i in lst2: 27 print(i[
运行截图:
实验任务十
task_1
1 import datetime 2 t = datetime.datetime.now() 3 filename = t.strftime('%Y%m%d') + '.txt' 4 n=eval(input('输入随机抽取人数:')) 5 with open('data10_stu.txt', 'r', encoding = 'utf-8') as f: 6 data=f.readlines() 7 8 9 def random_selection(n): 10 import random 11 demo = random.sample(data, n) 12 return demo 13 x=random_selection(n) 14 for i in x : 15 print(i) 16 17 18 with open(filename,'w',encoding='utf-8')as f: 19 f.writelines(x)
task_2
实验源码:
1 import datetime 2 t = datetime.datetime.now() 3 filename = t.strftime('%Y%m%d') + '.txt' 4 start='抽取开始' 5 print(start.center(50,'*')) 6 with open('data10_stu.txt', 'r', encoding='utf-8') as f: 7 data = f.readlines() 8 9 def random_selection(n): 10 import random 11 demo = random.sample(data, n) 12 return demo 13 selection=set() 14 numbers=[] 15 16 while True: 17 n = eval(input('输入随机抽点人数:')) 18 if n != 0: 19 x=random_selection(n) 20 for i in x: 21 print(i) 22 data.remove(i) 23 with open(filename,'a',encoding='utf-8') as f : 24 f.writelines(i) 25 else: 26 print('抽取结束'.center(50,'*')) 27 break
标签:info,encoding,title,实验,print,open,append From: https://www.cnblogs.com/zyaiyj-www-2003/p/17462475.html