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

实验5 文件应用编程

时间:2023-06-06 19:22:29浏览次数:41  
标签:文件 encoding open 编程 len 实验 print data append

实验任务6

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

 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:

1 with open('hamlet.txt', 'r+', encoding = 'utf-8') as f:
 2     data=f.read()
 3     data0_5=str(data)
 4     data1=data0_5.split("\n")
 5     data1_5=" ".join(data1)
 6     print(len(data1))
 7     data2=data0_5.split(" ")
 8     print(len(data2)-1)
 9     print(data.count(" ")-1)
10     print(len(data))
11     s=[]
12     s.append(len(data1))
13     s.append(len(data2)-1)
14     s.append(data.count(" ")-1)
15     s.append(len(data))
16     b=[]
17     for i in range(len(data1)):
18         b.append(f"{i+1} "+data1[i])
19     print(b)
20     with open('hamlet_with_line_number.txt', 'w', encoding = 'utf-8') as g:
21         for i in range(len(b)):
22             g.write(b[i]+"\n")

 

实验任务9

1 def is_valid(number):
 2     if len(number) == 18:
 3         return True
 4     elif (number[0:-1] is type(int) and number[-1] == 'X') or number[0:] is type(int) :
 5         return True
 6     else:
 7         return False
 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])

 

实验任务10

 

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

 

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

 

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

相关文章

  • 特殊文件、日志技术概括
       ......
  • 实验5
    task6withopen('data6.csv','r',encoding='gbk')asf:old_data=f.read().split('\n')delold_data[0]processed_data=[]foriinrange(len(old_data)):ifeval(old_data[i])+0.5>=int(eval(old_data......
  • docker 操作nginx命令+docker-compose常用命令及yml文件编写
    docker-compose常用命令及yml文件编写https://blog.csdn.net/doubiy/article/details/118997661 https://docs.docker.com/compose/1.观察下载容器镜像过程dockerrun-dnginx:latest-d表示当前终端的后台运行nginx:latest就是最新的nginx版本2.访问容器中的ngi......
  • Idea修改html文件不需重启
    1.首先设置为自动编译“File”->“Settings”->“Build,Execution,Deplyment”->“Compiler”2.允许运行时自动编译“Shift+Ctrl+Alt+/”,选择“Registry”,选中打勾“compiler.automake.allow.when.app.running”......
  • 实验5
    task61withopen('data6.csv','r',encoding='gbk')asf:2old_data=f.read().split('\n')3delold_data[0]45new_data=[]6foriinrange(len(old_data)):7ifeval(old_data[i])+float(0.5)......
  • 实验五
    6:1withopen('data6.csv','r',encoding='gbk')asf:2raw_data=f.read().split('\n')3title1=['原始数据']45data1=[float(i)foriinraw_data[1:]]6print(f'{title1[0]}:')......
  • 实验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......
  • Linux 创建用户、赋予用户权限、修改文件夹权限、修改只读文件
    创建用户#useraddwork设置用户密码#passwdwork赋予用户权限方法一:修改/etc/sudoers文件,找到下面一行,把前面的注释(#)去掉##Allowspeopleingroupwheeltorunallcommands%wheelALL=(ALL)ALL然后修改用户,使其属于root组(wheel),命令如下:#usermod-grootwork......
  • fluent读取网格文件报错:eof inside list是什么意思,怎么解决?
    做好二维网格之后,导入fluent中网格文件,发现报错:eofinsidelisteofinsidelist这个一般是指格式出错,应该是网格中的一些问题,可以检查一遍网格,尤其是对于边界条件的定义方面,我检查了一遍发现是网格边界在命名的时候出现了非法字符:;,重新修改名称之后就没有问题了。......
  • 实验六
    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)......