首页 > 编程语言 >Python实验课5

Python实验课5

时间:2023-06-05 16:13:29浏览次数:39  
标签:encoding Python open len sum1 print data 实验课

实验任务六

实验源码:

 1 with open('data6.csv','r',encoding='gbk') as f:
 2     data = f.readlines()
 3 data1=[str(eval(data[i])) for i in range(1,len(data))]
 4 data2=[str(int(eval(data[i])+0.5)) for i in range(1,len(data))]
 5 info=[[data1[i],data2[i]] for i in range(len(data1)) ]
 6 title = ['原始数据','四舍五入后数据']
 7 
 8 with open('data6_processed.csv', 'w', encoding = 'gbk') as f:
 9     f.write(','.join(title) + '\n') # 写入标题行
10     for item in info:
11         f.write(','.join(item) + '\n')
12 ls1 = []
13 ls2 = []
14 with open('data6_processed.csv', 'r', encoding = 'gbk') as f:
15     data3=f.readlines()
16 for i in range(1,len(data3)):
17     data4=data3[i].split(',')
18     ls1.append(data4[0].strip())
19     ls2.append(data4[1].strip())
20 print('原始数据')
21 print(ls1)
22 print('四舍五入后数据')
23 print(ls2)

 

  

实验结果截图:


实验任务七

实验源码:

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

 

实验任务八:

实验源码:

 1 lines = 0
 2 words = 0
 3 sum1 = 0
 4 space = 0
 5 with open('hamlet.txt','r',encoding='utf-8') as f:
 6     for line in f:
 7         word = line.split()
 8         lines += 1
 9         words += len(word)
10         sum1 += len(line)
11         for i in line:
12             if i == ' ':
13                 space += 1
14             else:
15                 pass
16 print('hamlet.txt粗滤统计:')
17 print(f'行数:{lines}')
18 print(f'单词数:{words}')
19 print(f'字符数:{sum1}')
20 print(f'空格数:{space}')
21  
22 with open('hamlet.txt','r',encoding = 'utf-8') as f:
23     text = f.readlines()
24  
25 for i in range(len(text)):
26     text[i] = str(i+1) + ' ' + text[i]
27  
28 with open('hamlet_with_line_number.txt','w',encoding = 'utf-8') as f:
29     f.writelines(text)

 

实验结果截图:

实验任务九

实验源码:

 1 def is_valid(sfz):
 2     if len(sfz) != 18:
 3         return False
 4     elif (sfz[:-1].isnumeric() and sfz[-1] == 'X') or sfz.isnumeric():
 5         return True
 6     else:return False
 7  
 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])

 

 

实验任务十

task_1

实验源码:

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 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 sum1 = 0
17 for i in range(n):
18     print(data[x[i]])
19     x2 += data[x[i]]
20     sum1 += n
21 with open('20230602.txt','w',encoding='utf-8') as f:
22     f.writelines(x2)

 

实验结果截图:

task_2

实验源码:

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

 

实验结果截图:

标签:encoding,Python,open,len,sum1,print,data,实验课
From: https://www.cnblogs.com/MaYangJie/p/17458035.html

相关文章

  • python操作Excel,读取表格内容,并根据需求将内容填充到表格指定位置
    在日常工作中经常会遇到需要将“表一,部门信息表”内容逐行填充到“表二,个人基本信息表”的情况。表一:表二:  实现方式如下:#!usr/bin/python#-*-coding:utf-8-*-importpandasaspdimportopenpyxlasopfromopenpyxlimportWorkbookfromopenpyxlimportl......
  • 用 Python3 & OpenCV 将视频转成字符动画
    在介绍如何用Python3&OpenCV将视频转成字符动画之前,先简单的介绍一下OpenCV吧,毕竟可能很多小伙伴不太了解:百度百科:OpenCV是一个基于BSD许可(开源)发行的跨平台计算机视觉库,可以运行在Linux、Windows和MacOS操作系统上。它轻量级而且高效——由一系列C函数和少量C++类构成,......
  • python爬虫爬取快手视频多线程下载功能【fd的使用】
    环境:python2.7+win10工具:fiddlerpostman安卓模拟器首先,打开fiddler,fiddler作为http/https抓包神器,这里就不多介绍。配置允许https 配置允许远程连接也就是打开http代理 电脑ip:192.168.1.110然后确保手机和电脑是在一个局域网下,可以通信。由于我这边没有安卓......
  • pip 安装错误 Command "python setup.py egg_info" failed with error code
    pythonsetup.pyinstallFile"setup.py",line7defread(rel_path:str)->str:^SyntaxError:invalidsyntax wgethttps://bootstrap.pypa.io/pip/2.7/get-pip.pypythonget-pip.pypipinstall--upgradesetuptools......
  • python xml 响应断言
    XML断言fromrequests_xmlimportXMLSessionsession=XMLSession()r=session.get('https://www.nasa.gov/rss/dyn/lg_image_of_the_day.rss')print(r.xml.links)XPath断言fromrequests_xmlimportXMLSessionsession=XMLSession()r=session.get(�......
  • 【python】打包py文件
    pyinstaller安装方法步骤:①安装打包用的模块pipinstallPyInstaller②创建虚拟环境pipinstallpipenv#用于搭建虚拟环境pipenvinstall#创建一个新的虚拟环境pipenvshell#进入这个虚拟环境pipinstall--#通过pip安装程序所需要的模块PyInstaller-i1.i......
  • 16个好用到爆的Python实用技巧!
    介绍人生苦短,快学Python!Python是一门用途广泛的编程语言,它具有大量的库和框架。有一些鲜为人知的Python编码技巧和库可以让你作为开发人员的工作更为轻松,编写代码更高效。本文将探讨一些鲜为人知的Python技巧,这些技巧非常有用,但并不广为人知。通过学习和使用这些技巧,可以......
  • 学习Python能做什么 怎么快速学习学习Python
    学习Python能做什么?Python是一个高层次的结合了解释性、编译性、互动性和面向对象的脚本语言,设计具有很强的可读性。相比其他语言经常使用英文关键字,其他语言的一些标点符号,Python具有比其他语言更有特色语法结构。Python被称为“胶水语言”,拥有丰富而强大的库,在数据分析、数据挖......
  • 实验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)......
  • 补充:通过Python中的subprocess模块实现安装pip模块
    通过Python代码实现安装pip模块或更新可以使用Python的内置模块subprocess来实现安装pip模块,具体步骤如下:【一】导入subprocess模块。importsubprocess【二】执行安装命令。subprocess.call(['python','-m','ensurepip','--upgrade'])在上面的代码中,调用subproce......