首页 > 其他分享 >实验5

实验5

时间:2023-06-05 16:33:48浏览次数:41  
标签:encoding data len 实验 sum1 print open

with open('data6.csv','r',encoding='gbk') as f:
    data = f.readlines()
data1=[str(eval(data[i])) for i in range(1,len(data))]
data2=[str(int(eval(data[i])+0.5)) for i in range(1,len(data))]
info=[[data1[i],data2[i]] for i in range(len(data1)) ]
title = ['原始数据','四舍五入后数据']

with open('data6_processed.csv', 'w', encoding = 'gbk') as f:
    f.write(','.join(title) + '\n') # 写入标题行
    for item in info:
        f.write(','.join(item) + '\n')
ls1 = []
ls2 = []
with open('data6_processed.csv', 'r', encoding = 'gbk') as f:
    data3=f.readlines()
for i in range(1,len(data3)):
    data4=data3[i].split(',')
    ls1.append(data4[0].strip())
    ls2.append(data4[1].strip())
print('原始数据')
print(ls1)
print('四舍五入后数据')
print(ls2)

 

 

 

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

 

 

lines = 0
words = 0
sum1 = 0
space = 0
with open('hamlet.txt','r',encoding='utf-8') as f:
    for line in f:
        word = line.split()
        lines += 1
        words += len(word)
        sum1 += len(line)
        for i in line:
            if i == ' ':
                space += 1
            else:
                pass
print('hamlet.txt粗滤统计:')
print(f'行数:{lines}')
print(f'单词数:{words}')
print(f'字符数:{sum1}')
print(f'空格数:{space}')

with open('hamlet.txt','r',encoding = 'utf-8') as f:
    text = f.readlines()

for i in range(len(text)):
    text[i] = str(i+1) + ' ' + text[i]

with open('hamlet_with_line_number.txt','w',encoding = 'utf-8') as f:
    f.writelines(text)

 

 

with open('data10_stu.txt','r',encoding='utf-8') as f:
    data = f.readlines()
print('{:*^40}'.format('抽点开始'))
n = int(input('输入随机抽点人数:'))
import random
x = []
sum1 = 0
x2 = ''
while n != 0:
    counts = 0
    while counts < n:
        new = random.randint(0, len(data) - 1)
        if new in x:
            new = random.randint(0, len(data) - 1)
        else:
            counts +=1
            x.append(new)
    for i in range(sum1,sum1+n):
        print(data[x[i]])
        x2 += data[x[i]]
    sum1 += n
    n = int(input('输入随机抽点人数:'))

with open('20230602.txt','w',encoding='utf-8') as f:
    f.writelines(x2)

print('{:*^40}'.format('抽点结束'))

 

 

标签:encoding,data,len,实验,sum1,print,open
From: https://www.cnblogs.com/zc135847/p/17458120.html

相关文章

  • 实验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......
  • 实验二——验收三
    验收三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=..(学号目录的绝对路......