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

实验5 文件应用编程

时间:2023-06-05 19:11:08浏览次数:54  
标签:文件 encoding open 编程 data 实验 print data1 data0

实验任务6

实验源码:

 1 with open('data6.csv', 'r', encoding='gbk') as f:
 2     raw_data = f.read().split('\n')
 3     title1 = ['原始数据']
 4 
 5 data1 = [float(i) for i in raw_data[1:]]
 6 print(f'{title1[0]}:')
 7 print(data1)
 8 
 9 data2 = [int(float(i)+0.5) for i in data1]
10 title2 = ['四舍五入后数据']
11 print(f'{title2[0]}:')
12 print(data2)
13 
14 with open('data6_processed.csv', 'w', encoding='gbk', newline='') as f:
15     f.write(title1[0] + ',' + title2[0] + '\n')
16     for i in range(len(data1)):
17         f.write(str(str(data1[i]) + ',' + str(data2[i]) + '\n'))

运行截图:

 

 

实验任务7

实验源码:

 1 with open('data7.csv', 'r', encoding='gbk') as f:
 2     data0 = f.readlines()
 3 
 4 data1 = [i.strip('\n').split(',') for i in data0]
 5 title1 = data1.pop(0)
 6 
 7 print(f'{title1[0]:10s}{title1[1]:10s}{title1[2]:10s}{title1[3]:10s}')
 8 
 9 data2 = sorted(data1, key=lambda x: (x[2], -int(x[3])))
10 for i in data2:
11     print(f'{i[0]:10s}{i[1]:10s}{i[2]:10s}{i[3]:10s}')
12 
13 
14 with open('data7_processed.csv', 'w', encoding='gbk') as f:
15     f.write(','.join(title1) + '\n')
16     for i in data2:
17         f.write(','.join(i) + '\n')

运行截图:

 

 

实验任务8

实验源码:

 1 with open('hamlet.txt', 'r', encoding='utf-8') as f:
 2     data = f.read()
 3 
 4 print('hamlet.txt粗略统计:')
 5 print(f'行数:{len(data.splitlines())}')
 6 print(f'单词数:{len(data.split())}')
 7 print(f'字符数:{len(data)}')
 8 print(f'空格数:{data.count(" ")}')
 9 
10 data1 = data.splitlines()
11 with open('hamlet_with_line_number.txt', 'w', encoding='utf-8') as f:
12     for number, words in enumerate(data1, 1):
13         f.write(f'{number}  {words}\n')

运行截图:

 

 

实验任务9

实验源码:

 1 import datetime
 2 
 3 
 4 def is_valid(x):
 5     if len(x) == 18 and x[:-1].isdigit() and (x[-1].isdigit() or x[-1] == 'X'):
 6         return True
 7     else:
 8         return False
 9 
10 
11 with open('data9_id.txt', 'r', encoding='utf-8') as f:
12     data0 = f.readlines()
13     title = data0.pop(0)
14 
15 data0 = [i.strip('\n').split(',') for i in data0]
16 
17 data1 = []
18 for i in data0:
19     if is_valid(i[1]):
20         data1.append(i)
21 print(data1)
22 
23 t = datetime.datetime.now()
24 y = t.strftime('%Y%m%d')
25 for i in data1:
26     age = str(int(y)-int(i[1][6:14]))
27     i.append(age[:2])
28     i[1] = i[1][6:10] + '-' + i[1][10:12] + '-' + i[1][12:14]
29 data2 = sorted(data1, key=lambda x: -int(x[2]))
30 
31 print('姓名, 出生日期, 年龄')
32 for i in data2:
33     print(', '.join(i))

运行截图:

 

 

实验任务10_1

实验源码:

 1 import random
 2 import datetime
 3 
 4 with open('data10_stu.txt', 'r', encoding='utf-8') as f:
 5     data = f.readlines()
 6 
 7 data0 = [i.strip('\n').split('\t') for i in data]
 8 n = eval(input('输入随机抽点人数:'))
 9 data1 = random.sample(data0, n)
10 
11 for i in data1:
12     print('  '.join(i))
13 
14 t = datetime.datetime.now()
15 title = t.strftime('%Y%m%d')
16 
17 with open(f'{title}.txt', 'w', encoding='utf-8') as f:
18     for i in data1:
19         f.write(f'{i[0]}\t{i[1]:6s}\t{i[2]:10s}\n')

运行截图:

 

 

实验任务10_2

实验源码:

 1 import random
 2 import datetime
 3 t = datetime.datetime.now()
 4 title = t.strftime('%Y%m%d')
 5 
 6 with open('data10_stu.txt', 'r', encoding='utf-8') as f:
 7     data = f.readlines()
 8 data0 = [i.strip('\n').split('\t') for i in data]
 9 
10 print(f'{"抽点开始":*^40}')
11 n = eval(input('输入随机抽点人数:'))
12 
13 a = 0
14 while n != 0:
15     data1 = random.sample(data0, n)
16     a += 1
17     for i in data1:
18         print('  '.join(i))
19         data0.remove(i)
20     if a == 1:
21         with open(f'{title}.txt', 'w', encoding='utf-8') as f:
22             for i in data1:
23                 f.write(f'{i[0]}\t{i[1]:6s}\t{i[2]:10s}\n')
24     else:
25         with open(f'{title}.txt', 'a', encoding='utf-8') as f:
26             for i in data1:
27                 f.write(f'{i[0]}\t{i[1]:6s}\t{i[2]:10s}\n')
28     n = eval(input('输入随机抽点人数:'))
29 print(f'{"抽点结束":*^40}')

运行截图:

 

标签:文件,encoding,open,编程,data,实验,print,data1,data0
From: https://www.cnblogs.com/xrl1113/p/17458711.html

相关文章

  • Python实验课5
    实验任务六实验源码:1withopen('data6.csv','r',encoding='gbk')asf:2data=f.readlines()3data1=[str(eval(data[i]))foriinrange(1,len(data))]4data2=[str(int(eval(data[i])+0.5))foriinrange(1,len(data))]5info=......
  • IO流:字符流-文件字符输入流
         ......
  • python文件路径和内容处理以及异常处理
    1.文件路径绝对路径和相对路径绝对路径:绝对路径,从盘符开始的路径(d:\data\file\abc.txt)frompathlibimportPathpath=Path('test.txt')print(path.absolute())相对路径:相对于当前的路径,当前是一个变量,在执行python时,所处的目录(./abc/a.txt ../ab/c.txttest.txt)没有......
  • C#异步编程2
                                  ......
  • 13. linux 中断式驱动编程
    1.根据硬件工程生成设备数文件dts/{amba_pl:amba_pl{#address-cells=<1>;#size-cells=<1>;compatible="simple-bus";ranges;axi_timer_0:timer@42800000{clock-frequency=<10000......
  • 使用 TypeScript 探索面向对象编程
    在软件开发领域,面向对象编程(OOP)已成为创建复杂且可扩展应用程序的基本范例。支持OOP概念的最流行的编程语言之一是TypeScript。TypeScript是JavaScript的超集,它增加了静态类型和其他功能以增强代码的可维护性和可读性。在这篇博客中,我们将探讨TypeScript中面向对象编程......
  • 全志科技官方Ubuntu16.04根文件系统镜像的替换和测试方法
     本指导文档主要基于全志A40i开发板——TLA40i-EVM,一款基于全志科技A40i处理器设计的4核ARMCortex-A7高性能低功耗国产评估板,演示Ubuntu根文件系统镜像的替换和测试方法。创龙科技TLA40i-EVM评估板接口资源丰富,引出双路网口、双路CAN、双路USB、双路RS485等通信接口,板载Bluetoo......
  • 实验五
    实验任务6task6:1withopen('data6.csv','r',encoding='gbk')asf:2data=list(f.readlines())3data=[i.strip('\n')foriindata]45importdecimal6decimal.getcontext().rounding='ROUND_HALF_UP'......
  • 实验五
    withopen('data6.csv','r',encoding='gbk')asf:data=f.readlines()processed_data=[]foriinrange(1,len(data)):row=data[i].strip()processed_item=round(float(row))processed_data.append(proces......
  • 实验5
    6.实验任务6程序源代码:withopen('data6.csv','r',encoding='gbk')asf:data=f.readlines()data_title=['原始数据','四舍五入后数据']data.remove('原始数据\n')data_zahl=[]foriindata:ifi[-1]==�......