首页 > 其他分享 >实验5

实验5

时间:2023-06-07 09:05:27浏览次数:42  
标签:open encoding data1 title 实验 print data

实验源码:

with open('data6.csv','r',encoding='gbk')as f:
     data=list(f.readlines())
  data=[i.strip('\n') for i in data ]
  
  import decimal
  decimal.getcontext().rounding='ROUND_HALF_UP'
  title=[data[0],'四舍五入后的数据']
  data_new=[]
 data_o=[]
 for i in data:
     if not i.isalpha() is True:
         data_o.append(eval(i))
 print('原始数据:')
 print(data_o)
 for i in data:
    if not i.isalpha() is True:
         data_new.append([i])

 data_=[]
 for i in data:
     if not i.isalpha() is True:
         i=eval(i)
         if i-int(i)>=0.5:
             i=int(i)+1
            data_new[index].append(str(i))
            data_.append(i)
         else:
             i=int(i)
             data_new[index].append(str(i))
             data_.append(i)
 print('四舍五入后的数据:')
 print(data_)
 import csv
 with open('data6_processed.csv','w',encoding='gbk',newline='') as f :
     f_writer=csv.writer(f)
     f_writer.writerow(title)
     f_writer.writerows(data_new)

实验源码:

1 with open('data7.csv','r',encoding='gbk') as f:
 2     data1 = f.read().split('\n')
 3 del data1[0]
 4 lsta = []
 5 lstb = []
 6 for i in data1:
 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]))

运行截图:

实验源码:

 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')

运行截图:

实验源码:

 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))

运行截图:

实验源码:

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')

运行截图:

实验源码:

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}')
复制代码

运行截图:

 

标签:open,encoding,data1,title,实验,print,data
From: https://www.cnblogs.com/L202283820006/p/17462277.html

相关文章

  • 实验7 面向对象编程与内置模块
    一、实验结论:1.实验任务1:task1.py程序源码:1'''2银行账户3数据:持卡人姓名、账号、当前余额4操作:取款、存款、打印账户信息、返回账户余额5'''67classAccount:8'''一个模拟银行账户的简单类'''910def__init__(self,name,account_nu......
  • 实验五
    实验任务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......
  • 实验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......