实验五 文件应用编程

task6

实验源码

 1 import csv
 2 
 3 title = ['原始数据', '四舍五入后的数据']
 4 
 5 lst = []
 6 yuan_lst = []
 7 hou_lst = []
 8 with open('data6.csv', 'r') as f:
 9     read_lst = f.readlines()
10     for i in range(1, len(read_lst)):
11         y = float(read_lst[i].strip('\n'))
12         yuan_lst.append(y)
13         c = int(y+0.5)
14         hou_lst.append(c)
15         lst.append([y, c])
16 
17 print(f'{title[0]}:\n{yuan_lst}')
18 print(f'{title[1]}:\n{hou_lst}')
19 
20 with open('data6_processed.csv', 'w', encoding='gbk', newline='')as f:
21     f_writer = csv.writer(f)
22     f_writer.writerow(title)
23     f_writer.writerows(lst)

 

运行测试截图

 

 

task7

实验源码

 1 import csv
 2 
 3 with open('data7.csv', 'r', encoding='gbk') as f:
 4     f_reader = list(csv.reader(f))
 5     title = f_reader.pop(0)
 6     for line in f_reader:
 7         line[3] = int(line[3])
 8     f_reader.sort(key=lambda x: (x[2], -x[3]))
 9 
10 with open('data7_processes.csv', 'w', encoding='gbk', newline='') as f:
11     f_writer = csv.writer(f)
12     f_writer.writerow(title)
13     f_writer.writerows(f_reader)
14 
15 with open('data7_processes.csv', 'r', encoding='gbk') as f:
16     print(f.read().replace(',', ' '*6))

 

运行测试截图

 

 

task8

实验源码

 1 with open('hamlet.txt', 'r') as f:
 2     w = f.read()
 3     line = w.count('\n') + 1
 4     print(f'行数:{line}')
 5     d = w.split()
 6     print(f'单词数:{len(d)}')
 7     print(f'字符数:{len(w)}')
 8     print(f'空格数:{w.count(" ")}')
 9     lst = w.split('\n')
10     for i in range(len(lst)):
11         lst[i] = f'{i+1:<4}.' + lst[i] + '\n'
12 
13 with open('hamlet_with_line_number.txt', 'w') as f:
14     f.writelines(lst)

 

运行测试截图

 

task9

实验源码

 1 import csv, datetime
 2 
 3 
 4 def is_valid(n):
 5     if len(n) == 18 and (n[:-1].isnumeric() and n[-1].upper() == 'X' or n.isnumeric()):
 6         return True
 7     else:
 8         return False
 9 
10 
11 with open('data9_id.txt', 'r', encoding='utf-8') as f:
12     f_reader = list(csv.reader(f))
13     title = "姓名, 出生日期, 年龄"
14     print(title)
15     lst = []
16     time = datetime.datetime.now()
17     y = int(time.strftime('%Y'))
18     rq = int(time.strftime('%m%d'))
19     for line in f_reader:
20         if is_valid(line[1]):
21             cs_rq = f'{line[1][6:10]}-{line[1][10:12]}-{line[1][12:14]}'
22             nl = y - int(line[1][6:10])
23             if rq < int(line[1][10:14]):
24                 nl -= 1
25             lst.append([line[0], cs_rq, nl])
26 
27 lst.sort(key=lambda x: x[2], reverse=True)
28 for item in lst:
29     print(f'{item[0]},{item[1]},{item[2]}')

 

运行测试截图

 

 

task10_1

实验源码

 1 import random
 2 
 3 n = int(input('输入随机抽点人数:'))
 4 lst = random.sample(list(range(80)), n)
 5 nd = []
 6 
 7 with open('data10_stu.txt', 'r', encoding='utf-8') as f:
 8     data = f.readlines()
 9     for i in lst:
10         nd.append(data[i])
11         print(f'{data[i]}', end='')
12 
13 with open('20230607.txt', 'w') as f:
14     f.writelines(nd)

 

运行测试截图

 

 

task10_2

实验源码

 

 1 import random, datetime
 2 
 3 n = int(input('输入随机抽点人数:'))
 4 t = datetime.datetime.now().strftime('%Y%m%d')
 5 nd = []
 6 while n != 0:
 7     ls = list(range(80))
 8     lst = random.sample(ls, n)
 9 
10     with open('data10_stu.txt', 'r', encoding='utf-8') as f:
11         data = f.readlines()
12         for i in lst:
13             ls.remove(i)
14             nd.append(data[i])
15             print(f'{data[i]}', end='')
16     n = int(input('输入随机抽点人数:'))
17 
18 with open(f'{t}.txt', 'w') as f:
19     f.writelines(nd)

 

 

运行测试截图

 

 

 

posted @ 2023-06-07 09:53  理理理  阅读(15)  评论(0)    收藏  举报