首页 > 其他分享 >实验五

实验五

时间:2023-06-06 23:55:25浏览次数:28  
标签:old open len 实验 print data append

实验任务6 task6.py

 1 with open('data6.csv', 'r', encoding='gbk') as f:
 2     old_data = f.read().split('\n')
 3 del old_data[0]
 4 processed_data = []
 5 for i in range(len(old_data)):
 6     if eval(old_data[i]) + 0.5 >= int(eval(old_data[i])) + 1:
 7         processed_data.append(str(int(eval(old_data[i])) + 1))
 8     else:
 9         processed_data.append(str(int(eval(old_data[i]))))
10 title = ['原始数据', '四舍五入后数据']
11 new = []
12 for i in range(len(old_data)):
13     new.append([old_data[i], processed_data[i]])
14 with open('data6_processed.csv','w',encoding = 'gbk') as f:
15     f.write(','.join(title)+'/n')
16     for i in new:
17         f.write(','.join(i) + '\n')
18 print('原始数据:')
19 print(old_data)
20 print(f'四舍五入后数据:')
21 print(new)

 

 

 

实验任务7 task7.py
 1 with open('data7.csv','r',encoding='gbk') as f:
 2     data = f.read().split('\n')
 3 del data[0]
 4 lsta = []
 5 lstb = []
 6 for i in data:
 7     lst1 = i.split(',')
 8     if lst1[2] == 'Acting':
 9         lsta.append(lst1)
10     else:
11         lstb.append(lst1)
12 lstb.sort(key=lambda x:x[-1],reverse = True)
13 lsta.sort(key=lambda x:x[-1],reverse = True)
14 info = lsta + lstb
15 title = ['学号','姓名','专业','分数']
16 with open('data7_processed.csv','w',encoding='gbk') as f:
17     f.write(','.join(title)+'\n')
18     for items in info:
19         f.write(','.join(items)+'\n')
20 print('{:<10}'.format(title[0]),'{:<10}'.format(title[1]),'{:<10}'.format(title[2]),'{:<15}'.format(title[3]))
21 for i in info:
22     print('{:<10}'.format(i[0]),'{:<10}'.format(i[1]),'{:<10}'.format(i[2]),'{:<15}'.format(i[3]))

 

 

实验任务8 task8.py
 1 with open('hamlet.txt','r') as f:
 2     data = f.read()
 3 print('hamlet.txt粗略统计:')
 4 data1 = data.splitlines()
 5 print(f'行数:{len(data1)}')
 6 print(f'单词数:{len(data.split())}')
 7 print(f'字符数:{len(data)}')
 8 k = data.count(' ')
 9 print(f'空格数:{k}')
10  
11 data2=[]
12 for i in range(len(data1)):
13     data2.append(f'{i+1} '+ data1[i] + '\n')14
14 with open('hamlet_with_line_number.txt','w') as f:
15     f.writelines(data2)

 

 

 

实验任务9 task9.py
 1 def is_valid(a):
 2     if len(a) != 18:
 3         return False
 4     elif (a[:-1].isnumeric() and a[-1] == 'X') or (a.isnumeric()):
 5         return True
 6     else:
 7         return False
 8 
 9 with open('data9_id.txt','r') as f:
10     data = f.read().split('\n')
11 del data[0]
12 data1 = []
13 print('姓名,出生日期,年龄')
14 for i in data:
15     lst = i.split(',')
16     data1.append(lst)
17 lst1 = []
18 import datetime
19 t = datetime.datetime.now().year
20 for i in data1:
21     if is_valid(i[1]) == True:
22         name = i[0]
23         btd = i[1][6:14]
24         age = str(int(t)-int(i[1][6:10]))
25         lst1.append([name,btd,age])
26 lst1.sort(key=lambda x:x[2],reverse=True)
27 for i in lst1:
28     print(i[0],end=',')
29     print(f'{i[1][:4]}-{i[1][4:6]}-{i[1][6:8]}',end=',')
30     print(i[2])

 

 

实验任务10 task10_1.py
 1 with open('data10_stu.txt','r',encoding='utf-8') as f:
 2     data = f.readlines()
 3 
 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 
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 
 4 data1 = [i.strip('\n').split(',') for i in data]
 5 
 6 print('{:*^40}'.format('抽点开始'))
 7 n = int(input('输入随机抽点人数:'))
 8 d= []
 9 while n!=0:
10     import random
11     x = list(range(len(data1)))
12     t = random.sample(x,n)
13     for i in t:
14         d.append(data[i])
15         for j in data1[i]:
16             print(j)
17     n = int(input('输入随机抽点人数:'))
18 else:
19     print('{:*^40}'.format('抽点结束'))
20 
21 
22 with open('20230605.txt','w',encoding='utf-8') as f:
23     for i in d:
24         f.write(''.join(i)+'\n')

 

标签:old,open,len,实验,print,data,append
From: https://www.cnblogs.com/zjyuan1013/p/17462115.html

相关文章

  • 实验六
    实验任务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......
  • 实验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......
  • 实验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......
  • 实验5 文件应用编程
    task6源代码:withopen('data6.csv','r',encoding='gbk')asf:data1=f.read().split('\n')deldata1[0]foriinrange(len(data1)):data1[i]=eval(data1[i])data1[i]=float(data1[i])print('原始数据:&#......