首页 > 其他分享 >实验五

实验五

时间:2023-06-06 19:23:01浏览次数:33  
标签:open encoding data1 实验 print data data2

task6

源代码:

with open ('data6.csv','r',encoding='gbk') as f:
    data=f.readlines()

data.remove('原始数据\n')
data1=[]
for i in data:
    if i[-1]=='\n':
        i=i[:-1]
    data1.append(float(i))
print('原始数据:')
print(data1)

data2=[round(i+0.2) for i in data1]
print('四舍五入后数据:')
print(data2)

with open('data6_processed.csv','w',encoding='gbk') as f:
    f.write('原始数据,四舍五入后数据\n')
    for i in range(len(data2)):
        f.write(str(data1[i])+','+str(data2[i])+'\n')

运行结果截图:

task7

源代码:

with open('data7.csv','r',encoding='gbk') as f:
    data=f.readlines()

data1=[i.strip('\n').split(',') for i in data]
data1.remove(data1[0])

data_acting=[]
data_music=[]
for i in data1:
    if i[2]=='Acting':
        data_acting.append(i)
    elif i[2]=='Music':
        data_music.append(i)

data2=sorted(data_acting,key=lambda x:x[3],reverse=True)
data3=sorted(data_music,key=lambda x:x[3],reverse=True)
data4=data2+data3

with open('data7_processed.csv','w',encoding='gbk') as f:
    f.write('学号,姓名,专业,分数\n')
    for i in range(len(data4)):
        f.write(','.join(data4[i])+'\n')

运行结果截图

task8

源代码

with open('hamlet.txt','r',encoding='utf-8')as f:
    data=f.readlines()
    print('行数:',len(data))

x=''
for line in data:
    x+=line
alpha=0
b=0
for i in x:
    if i.isalpha():
        alpha+=1
    else:
        b+=1

word=len(x.split())
print('字符数:',alpha+b)
print('单词数:',word)
print('空格数:',x.count(' '))
data = [line.strip('\n').split(',') for line in data]
with open('hamlet_with_line_number','w',encoding='utf-8') as f:
    for i,j in enumerate(data,start=1):
        j=j[0]
        f.write("{}{}{}".format(i,' ',j)+'\n')

运行结果截图

task9

源代码

def is_valid(id):
    if len(list(id)) != 18:
        return False
    elif str(id).isnumeric() or (list(id)[-1]=='X' and str(id)[:-1].isnumeric()):
        return True
    else:
        return False


with open('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:x[2],reverse=True)

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

运行结果截图

task10

源代码

#10-1
import datetime
t = datetime.datetime.now()
filename = t.strftime('%Y%m%d') + '.txt'
n=eval(input('输入随机抽取人数:'))
with open('data10_stu.txt', 'r', encoding = 'utf-8') as f:
    data=f.readlines()


def random_selection(n):
    import random
    demo = random.sample(data, n)
    return demo
x=random_selection(n)
for i in x :
    print(i)

运行结果截图

源代码

#10-2
import random
import datetime

print('*'*20,'抽点开始','*'*20)
with open('data10_stu.txt','r',encoding='UTF-8')as f:
    data=f.readlines()
data1=[i.strip('\n').split('\t') for i in data]

t=datetime.datetime.now()
d=t.strftime('%Y%m%d')

n=int(input('输入随机抽点人数:'))
x=0
while n!=0:
    data2=random.sample(data1,n)
    for i in data2:
        print(' '.join(i))
        data1.remove(i)
    if x==1:
        with open('{}.txt'.format(d),'w',encoding='UTF-8') as f:
            for i in data2:
                i[0] += '\t'
                i[1] += '\t'
                i[2] += '\n'
                f.writelines(i)
    else:
        with open('{}.txt'.format(d), 'a', encoding='UTF-8') as f:
            for i in data2:
                i[0] += '\t'
                i[1] += '\t'
                i[2] += '\n'
                f.writelines(i)

    n = int(input('输入随机抽点人数:'))
print('*'*20,'抽点结束','*'*20)

运行结果截图

 

标签:open,encoding,data1,实验,print,data,data2
From: https://www.cnblogs.com/wybpyx/p/17461483.html

相关文章

  • 实验5 文件应用编程
    实验任务61withopen('data6.csv','r',encoding='gbk')asf:2old_data=f.read().split('\n')3delold_data[0]4processed_data=[]5foriinrange(len(old_data)):6ifeval(old_data[i])+0.5>=......
  • 实验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......
  • 实验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......
  • 实验六
    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)......
  • 实验5
    task6withopen('data6.csv','r',encoding='gbk')asf:data=list(f.readlines())data=[i.strip('\n')foriindata]importdecimaldecimal.getcontext().rounding='ROUND_HALF_UP'title=[data[0],'四......
  • 《大学物理实验上》期末笔记(一)不确定度的计算
    《大学物理实验上》期末笔记(一)不确定度的计算什么是不确定度?​ ★不确定度表示测量值可能变动(不能确定)的范围,也是与测量结果相关的一个参数,用于合理表示由于测量误差的存在而对被测量值的不能肯定的程度。​ 简单来说,我们测得一组值,分别为\(x_1,x_2,x_3...x_n\),我们可以通过......
  • 经纬恒润整车在环测试实验室发布新技术特性
        作为国内最早开展整车在环仿真测试技术预研的企业之一,经纬恒润自2016年起,正式启动了整车在环仿真相关技术的预研工作。整车在环实现了从半实物仿真到机械混合仿真的跨步,将功能测试与性能测试打包整合,从整车完整的功能和机械表现,评价了车辆整体能力。近年来,伴随着汽车行......
  • 2023年5月31日吴曦远202283820011实验五
    task1_1.pycode:withopen('data1.txt','r',encoding='utf-8')asf:data=f.readlines()n=0print(data)forlineindata:ifnotline.strip()=='':n+=1print(n)output:note:ifdelet"not"......