首页 > 其他分享 >实验5

实验5

时间:2023-06-05 14:56:55浏览次数:40  
标签:encoding len stu 实验 print data1 data2

实验任务6

实验源码:

with open('C:/Users/Lenovo/Desktop/data6.csv','r',encoding='gbk')as f:
    data1 = f.read().split('\n')
del data1[0]
for i in range(len(data1)):
    data1[i] = eval(data1[i])
    data1[i] = float(data1[i])
print('原始数据:')
print(data1)
data2 = []
for i in range(len(data1)):
    data2.append(int(data1[i]+0.5))
print('四舍五入后数据:')
print(data2)
for i in range(len(data1)):
    data1[i] = str(data1[i])
for i in range(len(data1)):
    data2[i] = str(data2[i])
title = ['原始数据','四舍五入后数据']
info = []
for i in range(len(data1)):
    info.append([data1[i],data2[i]])
with open('data6_processed.csv','w',encoding='utf-8') as f:
    f.write(','.join(title)+'\n')
    for item in info:
        f.write(','.join(item)+'\n')

 

测试截图:

 

 

实验任务7

实验源码:

with open("C:/Users/Lenovo/Desktop/data7.csv",'r',encoding='gbk')as f:
    title=[f.readline()]
    data=f.readlines()
def getscore(ls):
    return ls[len(ls)-1]

data=[line.strip('\n').split(',')for line in data]

data.sort(key=lambda x:(x[2],-int(x[3])))


with open('data7_processed.csv', 'w', encoding = 'gbk') as f:
    f.write(','.join(title))
    for item in data:
        f.write(','.join(item) + '\n')
title=title[0].strip('\n').split(',')

print('{:<10}'.format(title[0]),'{:<10}'.format(title[1]),'{:<10}'.format(title[2]),'{:<15}'.format(title[3]))
for i in data:
    print('{:<10}'.format(i[0]), '{:<10}'.format(i[1]), '{:<10}'.format(i[2]), '{:<10}'.format(i[3]))

 

测试截图:

 

 

实验任务8

实验源码:

with open("C:/Users/Lenovo/Desktop/hamlet.txt",'r',encoding='utf-8')as f:
    data1=f.readlines()
print('行数:',len(data1))
with open("C:/Users/Lenovo/Desktop/hamlet.txt",'r',encoding='utf-8')as f:
    data2=f.read()
print('单词数:',len(data2.split()))
isalpha=0
others=0
for i in data2:
    if i.isalpha():
        isalpha+=1
    else:
        others+=1
print('字符数:',isalpha+others)
print('空格数:',data2.count(' '))

with open('halmet_with_line_number.txt','w',encoding='utf-8')as f:
    data3=data2.splitlines()
    data4=[]
    for i in range(len(data3)):
        data4.append(f'{i+1} '+data1[i]+'\n')
    f.writelines(data4)

 

测试截图:

 

 

实验任务9

实验源码:

def is_valid(id):
    if len(id)==18 and id[:-1].isdigit() and (id[-1].isdigit() or id[-1]=='X'):
        return True
    else:
        return False

with open("C:/Users/Lenovo/Desktop/data9_id.txt",'r',encoding='utf-8')as f:
    data=f.readlines()

data.remove(data[0])
data0=[i.strip('\n').split(',') for i in data]
data1=[]
for i in data0:
    if is_valid(i[1])==True:
        i[1]=i[1][6:14]
        data1.append(i)

import datetime
t=datetime.datetime.now()
y=t.strftime('%Y%m%d')
for i in data1:
    i.append(str(int(y)-int(i[1][0:8])))

data2=sorted(data1,key=lambda x:-int(x[2]))

print('姓名, 出生日期, 年龄')
for i in data2:
    i[1]=i[1][0:4]+'-'+i[1][4:6]+'-'+i[1][6:8]
    i[2]=i[2][0:2]
    print(', '.join(i))

 

测试截图:

 

 

实验任务10(1)

实验源码:

测试截图:

 

实验任务10(2)

实验源码:

print(f"{'抽点开始':*^40}")
import random as r
with open("C:/Users/Lenovo/Desktop/data10_stu.txt",'r',encoding = 'utf-8') as f:
        stu = f.readlines()

def func(n):
    stu_list = []
    while True:
        if n != 0:
            i = r.randint(0,len(stu)-1)
            if stu[i] not in stu_list:
                stu_list.append(stu[i])
                print(stu[i])
                n -= 1
            else:
                pass
        else:
            print(f"{'抽点结束':*^40}")
            break

n = int(input('输入随机抽点人数:'))
func(n)

 

测试截图:

 

实验任务10(2)

实验源码:

print(f"{'抽点开始':*^40}")
import random as r
with open("C:/Users/Lenovo/Desktop/data10_stu.txt",'r',encoding = 'utf-8') as f:
        stu = f.readlines()

def func(n):
    stu_list = []
    while True:
        if n != 0:
            i = r.randint(0,len(stu)-1)
            if stu[i] not in stu_list:
                stu_list.append(stu[i])
                print(stu[i])
                n -= 1
            else:
                pass
        if n==0:
            break

while True:
    n = int(input('输入随机抽点人数:'))
    if n != 0:
        func(n)
    else:
        print(f"{'抽点结束':*^40}")
        break

 

测试截图:

 

标签:encoding,len,stu,实验,print,data1,data2
From: https://www.cnblogs.com/qwertasd123/p/17457708.html

相关文章

  • 实验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......
  • 实验二——验收三
    验收三1.你们小组项目中为了保护数据资产用了什么密码算法?2.如果用到了对称算法,提交相关生成密钥和对数据加密的代码截图3.如果用到了非对称算法,提交相关生成密钥对和对数据加密,签名验签的代码截图4.如果用到了其他算法,提交相关的代码截图1.算法使用①口令哈希值:sm3......
  • 计算机网络 实验一
    实验一vlan的创建与划分一、实验目的: 1.了解vlan的工作原理;2.学习基于端口划分vlan的方法;3.了解跨交换机的相同vlan之间的通信;4.进一步学习交换机端口的配置命令。二、实验原理:VLAN(VirtualLocalAreaNetwork)是一种虚拟局域网技术,允许将物理网络划分为逻辑上独立的多个虚拟网......
  • 实验一 密码引擎-1-OpenEuler-OpenSSL编译
    一、任务详情安装Ubuntu和OpenEuler虚拟机下载最新的OpenSSL源码(1.1版本)用自己的8位学号建立一个文件夹,cd你的学号,用pwd获得绝对路径参考https://www.cnblogs.com/rocedu/p/5087623.html先在Ubuntu中完成OpenSSL编译安装,然后在OpenEuler中重现./config--prefix=..(......
  • 实验一 密码引擎-2-电子钥匙功能测试
    实验一密码引擎-3-电子钥匙功能测试1解压“龙脉密码钥匙驱动实例工具等”压缩包2在Ubuntu中运行“龙脉密码钥匙驱动实例工具等\mToken-GM3000\skf\samples\linux_mac”中例程,提交运行结果截图3加分项:运行“龙脉密码钥匙驱动实例工具等\mToken-GM3000\skf\samples\windows......
  • 实验一 密码引擎-1-OpenEuler-OpenSSL编译
    安装Ubuntu和OpenEuler虚拟机下载最新的OpenSSL源码(1.1版本)用自己的8位学号建立一个文件夹,cd你的学号,用pwd获得绝对路径参考https://www.cnblogs.com/rocedu/p/5087623.html先在Ubuntu中完成OpenSSL编译安装,然后在OpenEuler中重现./config--prefix=..(学号目录的绝对路......
  • 实验一 密码引擎-2-电子钥匙功能测试
    0参考附件中的视频1解压"资源"中“龙脉密码钥匙驱动实例工具等”压缩包2在Ubuntu中运行“龙脉密码钥匙驱动实例工具等\mToken-GM3000\skf\samples\linux_mac”中例程,提交运行结果截图3加分项:运行“龙脉密码钥匙驱动实例工具等\mToken-GM3000\skf\samples\windows”中例程,......
  • 实验5 文件应用编程
    实验任务6源代码1withopen('data6.csv','r',encoding='gbk')asf:2data=list(f.readlines())3data=[i.strip('\n')foriindata]45importdecimal6decimal.getcontext().rounding='ROUND_HALF_UP'......
  • 实验二验收3
    实验二验收3......