首页 > 其他分享 >实验五

实验五

时间:2023-06-06 17:44:31浏览次数:31  
标签: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 with open('data7_processed.csv','w', encoding='gbk')as f:
14     f.write(','.join(title1)+'\n')
15     for i in data2:
16         f.write(','.join(i)+'\n')

截图:

8:

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

相关文章

  • 实验5
    #6实验内容:1#62withopen('data6.csv','r',encoding='gbk')asf:3f1=f.readlines()4f1=list(f1)5l1=[]6l2=[]7title=['原始数据','四舍五入后数据']8info1=f1[1:]9f2=[[str(eval(i)),str(round(e......
  • 实验六
    task1_1fromturtleimport*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(-200,0)draw(3)......
  • 实验5
    task6withopen('data6.csv','r',encoding='gbk')asf:data=list(f.readlines())data=[i.strip('\n')foriindata]importdecimaldecimal.getcontext().rounding='ROUND_HALF_UP'title=[data[0],'四......
  • 《大学物理实验上》期末笔记(一)不确定度的计算
    《大学物理实验上》期末笔记(一)不确定度的计算什么是不确定度?​ ★不确定度表示测量值可能变动(不能确定)的范围,也是与测量结果相关的一个参数,用于合理表示由于测量误差的存在而对被测量值的不能肯定的程度。​ 简单来说,我们测得一组值,分别为\(x_1,x_2,x_3...x_n\),我们可以通过......
  • 经纬恒润整车在环测试实验室发布新技术特性
        作为国内最早开展整车在环仿真测试技术预研的企业之一,经纬恒润自2016年起,正式启动了整车在环仿真相关技术的预研工作。整车在环实现了从半实物仿真到机械混合仿真的跨步,将功能测试与性能测试打包整合,从整车完整的功能和机械表现,评价了车辆整体能力。近年来,伴随着汽车行......
  • 2023年5月31日吴曦远202283820011实验五
    task1_1.pycode:withopen('data1.txt','r',encoding='utf-8')asf:data=f.readlines()n=0print(data)forlineindata:ifnotline.strip()=='':n+=1print(n)output:note:ifdelet"not"......
  • 实验5
    实验任务6withopen('C:/Users/Lenovo/Desktop/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])p......
  • 实验6 turtle绘图和Python库应用编程体验
    实验任务1task1_1.py源代码1fromturtleimport*23defmove(x,y):4penup()5goto(x,y)6pendown()78defdraw(n,size=100):9foriinrange(n):10fd(size)11left(360/n)1213defmain():14pensize(2)1......
  • 实验5
     with open('data6.csv','r',encoding='gbk') as f:  data1 = f.read().split('\n')del data1[0]print('原始数据:')print(data1)for i in range(len(data1)):    data1[i] = float(data1[i])data2 = []for i in ra......
  • 实验6 turtle绘图与python库应用编程体验
    实验任务1task1_11fromturtleimport*23defmoveto(x,y):4'''5画笔移动到坐标(x,y)处6'''7penup()8goto(x,y)9pendown()1011defdraw(n,size=100):12'''13绘制边长为s......