首页 > 编程语言 >实验五 文件应用编程

实验五 文件应用编程

时间:2023-06-07 10:01:38浏览次数:30  
标签:文件 10 编程 lst 实验 print line csv open

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)

 

 

运行测试截图

 

 

 

标签:文件,10,编程,lst,实验,print,line,csv,open
From: https://www.cnblogs.com/llldbk/p/17462083.html

相关文章

  • Python写文件时加锁,避免写入过程中被读取
    问题:Linux上有个Python2脚本每天定时生成一些数据,并覆盖写入文件A,文件内容是每行一个Json字符串。有一个乙方的采集器程序(类似filebeat)需要读取文件A,但发现读取的数据有截断,导致解析失败。怀疑是因为采集器读取文件A的时候,文件A写入还未结束。解决:由于采集器程序是乙方自研的,属......
  • 实验5
    task61importcsv2title=[]3info=[]45withopen('data6.csv','r',encoding='gbk')asf:6f_reader=csv.reader(f)7forlineinf_reader:8info.append(line)9x=info.pop(0)10title.appe......
  • 实验五
    任务6:withopen('data6.csv','r',encoding='gbk')asf:data=list(f.readlines())data=[i.strip('\n')foriindata]importdecimaldecimal.getcontext().rounding='ROUND_HALF_UP'title=[data[0......
  • 实验5
    实验源码:withopen('data6.csv','r',encoding='gbk')asf:data=list(f.readlines())data=[i.strip('\n')foriindata]importdecimaldecimal.getcontext().rounding='ROUND_HALF_UP'title=[data[......
  • 实验7 面向对象编程与内置模块
    一、实验结论:1.实验任务1:task1.py程序源码:1'''2银行账户3数据:持卡人姓名、账号、当前余额4操作:取款、存款、打印账户信息、返回账户余额5'''67classAccount:8'''一个模拟银行账户的简单类'''910def__init__(self,name,account_nu......
  • 几种分布式文件系统的优缺点归纳与总结
    1、常用的分布式文件系统有以下几种:1.HadoopHDFS:HadoopDistributedFileSystem(HDFS)是ApacheHadoop生态系统的一部分,用于存储和处理大数据。2.Ceph:Ceph是一个开源的分布式存储系统,提供了高可用性、高性能和可扩展性。3.GlusterFS:GlusterFS是一个开源的分布式文件系统,提供......
  • 实验五
    实验任务6task6.py1withopen('data6.csv','r',encoding='gbk')asf:2old_data=f.read().split('\n')3delold_data[0]4processed_data=[]5foriinrange(len(old_data)):6ifeval(old_data[i])......
  • 实验六
    实验任务1-1实验源码1fromturtleimport*2defmove(x,y):3penup()4goto(x,y)5pendown()6defdraw(n,size=100):7foriinrange(n):8fd(size)9left(360/n)10defmain():11pensize(2)12pencolo......
  • 实验五 文件应用编程
    task6.py1withopen('data6.csv','r',encoding='gbk')asf:2data1=f.read().split('\n')3deldata1[0]4foriinrange(len(data1)):5data1[i]=eval(data1[i])6data1[i]=float(data1[i])7......
  • 实验6
    1.实验任务1task1_1.py程序源代码:fromturtleimport*defmove(x,y):'''画笔移动到坐标(x,y)处'''penup()goto(x,y)pendown()defdraw(n,size=100):'''绘制边长为size的正n变形'''foriinran......