实验任务6
1 with open('data6.csv','r',encoding = 'gbk') as f: 2 old_data = f.read().split('\n') 3 del old_data[0] 4 5 new_data = [] 6 for i in range(len(old_data)): 7 if eval(old_data[i]) + float(0.5) >= int(eval(old_data[i])) + 1: 8 new_data.append(str(int(eval(old_data[i]))+1)) 9 else: 10 new_data.append(str(int(eval(old_data[i])))) 11 12 title = ['原始数据','四舍五入后数据'] 13 data = [] 14 for i in range(len(old_data)): 15 data.append([old_data[i],new_data[i]]) 16 17 with open('data6.csv','w',encoding = 'gbk') as f: 18 f.write(','.join(title) + '\n') 19 for i in data: 20 f.write(','.join(i) + '\n') 21 print('原始数据:') 22 print(old_data) 23 print('四舍五入后数据:') 24 print(new_data)
实验任务7
1 with open('data7.csv','r',encoding='gbk')as f: 2 title=[f.readline()] 3 data=f.readlines() 4 def getscore(ls): 5 return ls[len(ls)-1] 6 7 data=[line.strip('\n').split(',')for line in data] 8 9 data.sort(key=lambda x:(x[2],-int(x[3]))) 10 11 12 with open('data7_processed.csv', 'w', encoding = 'gbk') as f: 13 f.write(','.join(title)) 14 for item in data: 15 f.write(','.join(item) + '\n') 16 title=title[0].strip('\n').split(',') 17 18 print('{:<10}'.format(title[0]),'{:<10}'.format(title[1]),'{:<10}'.format(title[2]),'{:<15}'.format(title[3])) 19 for i in data: 20 print('{:<10}'.format(i[0]), '{:<10}'.format(i[1]), '{:<10}'.format(i[2]), '{:<10}'.format(i[3]))
实验任务8
1 with open('hamlet.txt','r',encoding='utf-8')as f: 2 data=f.readlines() 3 print('行数:',len(data)) 4 5 x='' 6 for line in data: 7 x+=line 8 alpha=0 9 b=0 10 for i in x: 11 if i.isalpha(): 12 alpha+=1 13 else: 14 b+=1 15 16 word=len(x.split()) 17 print('字符数:',alpha+b) 18 print('单词数:',word) 19 print('空格数:',x.count(' ')) 20 data = [line.strip('\n').split(',') for line in data] 21 with open('hamlet_with_line_number','w',encoding='utf-8') as f: 22 for i,j in enumerate(data,start=1): 23 j=j[0] 24 f.write("{}{}{}".format(i,' ',j)+'\n')
实验任务9
def is_valid(sfz): if len(sfz) != 18: return False elif (sfz[:-1].isnumeric() and sfz[-1] == 'X') or sfz.isnumeric(): return True else:return False with open('data9_id.txt','r',encoding='utf-8') as f: data = f.read().split('\n') del data[0] data2 = [] data3 =[] print('姓名,出生日期,年龄') for i in data: lst = i.split(',') data2.append(lst) lst2 = [] for i in data2: if is_valid(i[1]) == True: name = i[0] btd = i[1][6:14] age = str(2023 - int(i[1][6:10])) lst2.append([name,btd,age]) lst2.sort(key=lambda x:x[2],reverse=True) for i in lst2: print(i[0],end=',') print(f'{i[1][:4]}-{i[1][4:6]}-{i[1][6:8]}',end=',') print(i[2])
实验任务10
task10_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)
task10_2
1 with open('data10_stu.txt','r',encoding='utf-8') as f: 2 data = f.readlines() 3 print('{:*^40}'.format('抽点开始')) 4 n = int(input('输入随机抽点人数:')) 5 import random 6 x = [] 7 sum1 = 0 8 x2 = '' 9 while n != 0: 10 counts = 0 11 while counts < n: 12 new = random.randint(0, len(data) - 1) 13 if new in x: 14 new = random.randint(0, len(data) - 1) 15 else: 16 counts +=1 17 x.append(new) 18 for i in range(sum1,sum1+n): 19 print(data[x[i]]) 20 x2 += data[x[i]] 21 sum1 += n 22 n = int(input('输入随机抽点人数:')) 23 24 with open('20230602.txt','w',encoding='utf-8') as f: 25 f.writelines(x2) 26 27 print('{:*^40}'.format('抽点结束'))
标签:文件,old,encoding,open,编程,实验,print,new,data From: https://www.cnblogs.com/xixihamugua/p/17459227.html