首页 > 其他分享 >实验五

实验五

时间:2023-06-05 17:37:24浏览次数:32  
标签:encoding open random 实验 print line data

实验任务6

task6:

 1 with open('data6.csv','r',encoding='gbk')as f:
 2     data=list(f.readlines())
 3 data=[i.strip('\n') for i in data ]
 4 
 5 import decimal
 6 decimal.getcontext().rounding='ROUND_HALF_UP'
 7 title=[data[0],'四舍五入后的数据']
 8 data_new=[]
 9 data_o=[]
10 for i in data:
11     if not i.isalpha() is True:
12         data_o.append(eval(i))
13 print('原始数据:')
14 print(data_o)
15 for i in data:
16     if not i.isalpha() is True:
17         data_new.append([i])
18 
19 data_=[]
20 for i in data:
21     if not i.isalpha() is True:
22         index=data.index(i)-1
23         i=eval(i)
24         if i-int(i)>=0.5:
25             i=int(i)+1
26             data_new[index].append(str(i))
27             data_.append(i)
28         else:
29             i=int(i)
30             data_new[index].append(str(i))
31             data_.append(i)
32 print('四舍五入后的数据:')
33 print(data_)
34 import csv
35 with open('data6_after.csv','w',encoding='gbk',newline='') as f :
36     f_writer=csv.writer(f)
37     f_writer.writerow(title)
38     f_writer.writerows(data_new)

运行结果:

 

 

实验任务7

task7:

 1 with open('data7.csv','r',encoding='gbk')as f:
 2     title=[f.readline()]
 3     data=f.readlines()
 4 def getscore(ls):
 5     return ls[len(ls)-1]
 6 
 7 data=[line.strip('\n').split(',')for line in data]
 8 
 9 data.sort(key=lambda x:(x[2],-int(x[3])))
10 
11 
12 with open('data7_processed.csv', 'w', encoding = 'gbk') as f:
13     f.write(','.join(title))
14     for item in data:
15         f.write(','.join(item) + '\n')
16 title=title[0].strip('\n').split(',')
17 
18 print('{:<10}'.format(title[0]),'{:<10}'.format(title[1]),'{:<10}'.format(title[2]),'{:<15}'.format(title[3]))
19 for i in data:
20     print('{:<10}'.format(i[0]), '{:<10}'.format(i[1]), '{:<10}'.format(i[2]), '{:<10}'.format(i[3]))

运行结果:

 

实验任务8

task8:

 1 with open('hamlet.txt','r',encoding='utf-8')as f:
 2     data=f.readlines()
 3     print('行数:',len(data))
 4 
 5 x=''
 6 for line in data:
 7     x+=line
 8 alpha=0
 9 b=0
10 for i in x:
11     if i.isalpha():
12         alpha+=1
13     else:
14         b+=1
15 
16 word=len(x.split())
17 print('字符数:',alpha+b)
18 print('单词数:',word)
19 print('空格数:',x.count(' '))
20 data = [line.strip('\n').split(',') for line in data]
21 with open('hamlet_with_line_number','w',encoding='utf-8') as f:
22     for i,j in enumerate(data,start=1):
23         j=j[0]
24         f.write("{}{}{}".format(i,' ',j)+'\n')

运行结果:

 

 

实验任务9

task9:

 1 with open('data9_id.txt','r',encoding='utf-8')as f:
 2     data=f.readlines()
 3 
 4 info=[line.strip('\n').split(',')for line in data]
 5 del info[0]
 6 
 7 def is_valid(id):
 8     y=id[:17]
 9     if (i in ['1','2','3','4','5','6','7','8','9','0','X'] for i in id) and len(id)==18 and y.isdigit() and (id[17]=='X' or id[17].isdigit()):
10         return True
11     else:
12         return False
13 print('{:<10} {:<10} {:<10} '.format('姓名','出生日期','年龄'))
14 import datetime
15 today=datetime.date.today()
16 valid_ls=[]
17 for ls in info:
18     if is_valid(ls[1]) is True:
19         y, m, d = ls[1][6:10], ls[1][10:12], ls[1][12:14]
20         birth = y + '-' + m + '-' + d
21         ls.append(birth)
22         del ls[1]
23         birthday=datetime.date(year=int(y),month=int(m),day=int(d))
24         age=str(round((today-birthday).days/365))
25         ls.append(age)
26         valid_ls.append(ls)
27 def takeThird(elem):
28     return elem[2]
29 valid_ls.sort(key=takeThird,reverse = True)
30 for ls in valid_ls:
31     for i in ls:
32         print('{:8}'.format(i),end='    ')
33     print()

运行结果:

 

 

实验任务10

task10_1:

 1 import datetime
 2 t = datetime.datetime.now()
 3 filename = t.strftime('%Y%m%d') + '.txt'
 4 n=eval(input('输入随机抽取人数:'))
 5 with open('data10_stu.txt', 'r', encoding = 'utf-8') as f:
 6     data=f.readlines()
 7 
 8 
 9 def random_selection(n):
10     import random
11     demo = random.sample(data, n)
12     return demo
13 x=random_selection(n)
14 for i in x :
15     print(i)
16 
17 
18 with open(filename,'w',encoding='utf-8')as f:
19     f.writelines(x)

运行结果:

task10_2:

 1 import datetime
 2 t = datetime.datetime.now()
 3 filename = t.strftime('%Y%m%d') + '.txt'
 4 start='抽取开始'
 5 print(start.center(50,'*'))
 6 with open('data10_stu.txt', 'r', encoding='utf-8') as f:
 7     data = f.readlines()
 8 
 9 def random_selection(n):
10     import random
11     demo = random.sample(data, n)
12     return demo
13 selection=set()
14 numbers=[]
15 
16 while True:
17     n = eval(input('输入随机抽点人数:'))
18     if n != 0:
19         x=random_selection(n)
20         for i in x:
21             print(i)
22             data.remove(i)
23             with open(filename,'a',encoding='utf-8') as f :
24                 f.writelines(i)
25     else:
26         print('抽取结束'.center(50,'*'))
27         break

运行结果:

 

标签:encoding,open,random,实验,print,line,data
From: https://www.cnblogs.com/gajs456asd5f4/p/17458476.html

相关文章

  • 实验五
    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]==�......
  • 实验6
      task5-1fromturtleimport*importmathdefmove(x,y):penup()goto(x,y)pendown()defmain():x=100*math.sqrt(2)pensize(2)move(-100,-100)begin_fill()forward(200)left(90)forward(200)left(90)......
  • 实验6 turtle绘图与python库应用编程体验
    任务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)mov......
  • 实验5
    withopen('data6.csv','r',encoding='gbk')asf:data=f.readlines()data1=[str(eval(data[i]))foriinrange(1,len(data))]data2=[str(int(eval(data[i])+0.5))foriinrange(1,len(data))]info=[[data1[i],data2[i]]forii......
  • 实验5 文件应用编程
    实验任务6:实验源码:withopen('data6.csv','r',encoding='gbk')asf:data=f.readlines()data1=[str(eval(data[i]))foriinrange(1,len(data))]data2=[str(int(eval(data[i])+0.5))foriinrange(1,len(data))]info=[[data1[i],dat......
  • 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=......
  • 实验5
    实验任务6实验源码:withopen('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......
  • 实验6 turtle绘图与python库应用编程体验
    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)......
  • 实验报告
    https://www.cnblogs.com/yycyhyhf/p/17264251.htmlhttps://www.cnblogs.com/yycyhyhf/p/17285673.htmlhttps://www.cnblogs.com/yycyhyhf/p/17378885.htmlhttps://www.cnblogs.com/yycyhyhf/p/17239138.htmlhttps://www.cnblogs.com/yycyhyhf/p/17327753.html......