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

实验五 文件应用编程

时间:2023-06-06 23:46:32浏览次数:38  
标签:文件 open encoding data 编程 len 实验 print data1

task6.py

 1 with open('data6.csv','r',encoding='gbk') as f:
 2     data1 = f.read().split('\n')
 3 del data1[0]
 4 for i in range(len(data1)):
 5     data1[i] = eval(data1[i])
 6     data1[i] = float(data1[i])
 7 print('原始数据:')
 8 print(data1)
 9 data2 = []
10 for i in range(len(data1)):
11     data2.append(int(data1[i]+0.5))
12 print('四舍五入后数据:')
13 print(data2)
14 for i in range(len(data1)):
15     data1[i] = str(data1[i])
16 for i in range(len(data1)):
17     data2[i] = str(data2[i])
18 title = ['原始数据','四舍五入后数据']
19 info = []
20 for i in range(len(data1)):
21     info.append([data1[i],data2[i]])
22 with open('data6_processed.csv','w',encoding='utf-8') as f:
23     f.write(','.join(title)+'\n')
24     for item in info:
25         f.write(','.join(item)+'\n')

task7.py

 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='utf-8') 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]))

task8.py

 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)

task9.py

 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[0],end=',')
28     print(f'{i[1][:4]}-{i[1][4:6]}-{i[1][6:8]}',end=',')
29     print(i[2])

task10_1.py

 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 x2 = ''
 8 counts = 0
 9 while counts < n:
10     new = random.randint(0, len(data) - 1)
11     if new in x:
12         new = random.randint(0, len(data) - 1)
13     else:
14         counts +=1
15         x.append(new)
16 for i in range(n):
17     print(data[x[i]])
18     x2 += data[x[i]]
19     sum1 += n
20 with open('20230602.txt','w',encoding='utf-8') as f:
21     f.writelines(x2)

task10_2.py

 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('抽点结束'))

 

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

相关文章

  • 实验6
    1.实验任务1task1_1.py程序源代码:fromturtleimport*defmove(x,y):'''画笔移动到坐标(x,y)处'''penup()goto(x,y)pendown()defdraw(n,size=100):'''绘制边长为size的正n变形'''foriinran......
  • Python | VSCode的Python编程环境搭建
    1、简介VScode是一款轻量化的代码编辑器,也是目前极为流行的适配性极强的写代码的工具,不仅仅可以用于C/C++代码项目的编写,还能够用于Python、java等其他语言。2、内容a、先下载安装Python的解释器b、安装VSCodec、安装Python相关的插件d、即可运行编译Python代码附:完整教程步骤......
  • 通过状态机方法实现基于FPGA的维特比译码器,包含testbench测试文件
    1.算法仿真效果vivado2019.2仿真结果如下:2.算法涉及理论知识概要viterbi译码算法是一种卷积码的解码算法。优点不说了。缺点就是随着约束长度的增加算法的复杂度增加很快。约束长度N为7时要比较的路径就有64条,为8时路径变为128条。(2<<(N-1))。所以viterbi译码一般应用在约束......
  • 实验6 turtle绘图与python库应用编程体验
    实验任务1task1_.py实验源码:fromturtleimport*defmove(x,y):penup()goto(x,y)pendown()defdraw(n,size=100):foriinrange(n):fd(size)left(360/n)defmain():pensize(2)pencolor('red')move(-20......
  • Linux查看和修改文件时间
    查看文件时间stat[文件路径]修改文件时间touch-d"2018-04-1808:00:00"[文件路径] 源:https://www.cnblogs.com/caoshousong/p/10730178.html......
  • 实验5
    task6源码:importcsvwithopen('data6.csv','r',encoding='gbk')asf:data_o=f.readlines()foriinrange(len(data_o)):data_o[i]=data_o[i].strip('\n')#原始数据deldata_o[0]#处理后数据data_1=[]ford_......
  • 实验五 文件应用编程
    实验五 文件应用编程 实验任务六程序源代码:1withopen('data6.csv','r',encoding='gbk')asf:2old_data=f.read().split('\n')3delold_data[0]4processed_data=[]5foriinrange(len(old_data)):6ifeval(old......
  • 实验5 文件应用编程
    实验任务1:统计文件行数(不统计空白行)task1_1.py'''统计文件data1.txt行数(不包括空白行)data1.txt中的空白行包括由空格、Tab键(\t)、换行(\n)构成的空白行'''withopen('data1.txt','r',encoding='utf-8')asf:data=f.readlines()n=0fo......
  • 野火STM32第35章 SDIO读写测试 实验过程犯的错误:卡在while(SD_GetStatus() != SD_TRAN
    看完书,我自己重新写了一遍代码。写代码过程中,对DMA以下标绿的部分很不以为意。1voidSD_DMA_TxConfig(uint32_t*DMA_Tx_Buf,uint32_tBufferSize)2{3DMA_InitTypeDefDMA_InitStruct;45/*ClockEnable-----InGPIOConfig*/67/*Flag......
  • 实验7 面向对象编程与内置模块
    task1.pycode:classAccount:def__init__(self,name,account_number,initial_amount=10):self._name=nameself._card_no=account_numberself._balance=initial_amountdefdeposit(self,amount):self._balance+=a......