首页 > 其他分享 >实验5

实验5

时间:2023-06-05 17:24:47浏览次数:39  
标签:open encoding data1 len 实验 print data

6.实验任务6

程序源代码:

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

data_title = ['原始数据','四舍五入后数据']

data.remove('原始数据\n')
data_zahl = []
for i in data:
    if i[-1]=='\n':
        i = i[:-1]
        data_zahl.append(float(i))
data_new = [int(i+0.5) for i in data_zahl]

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

 

运行程序截图:

 

 

7.实验任务7

 

程序源代码:

with open('data7.csv','r',encoding = 'gbk') as f:
    data = f.readlines()
data_t = ['学号','姓名','专业','分数']
data1 = [i.strip('\n').split(',') for i in data]
data1.remove(data1[0])

data2=sorted(data1,key=lambda x:(x[2],-int(x[3])))
with open('data7_processed.csv','w',encoding = 'gbk') as f:
    f.write(','.join(data_t) + '\n')
    for item in data2:
        f.write(','.join(item) + '\n')

print(f'{"学号":10s}{"姓名":10s}{"专业":10s}{"分数":10s}')
for a,b,c,d in data2:
    print('{:<10s}{:<10s}{:<10s}{:<10}'.format(a,b,c,d))

 

 

运行程序截图:

 

 

 

8.实验任务8

 

程序源代码:

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

print('hamlet.txt粗略统计:')
print(f'行数:{len(data.splitlines())}')
print(f'单词数:{len(data.split())}')
print(f'字符数:{len(data)}')
print(f'空格数:{data.count(" ")}')
data2 = []
for i in range(len(data1)):
    data2.append('{}'.format(i+1)+'.'+data1[i]+'\n')
with open('hamlet_with_line_number.txt','w',encoding='utf-8') as f:
    for i in data2:
        f.write(','.join(data2)+'\n')

 

 

运行程序截图:

 

 

 

9.实验任务9

程序源代码:

def is_valid(n):
    ls = []
    for i in range(10):
        ls.append(str(i))
    t = str(n)
    if len(t)!=18:
        return False
    else:
        i = 0
        while i <17:
            if t[i] not in ls:
                return False
                break
            else:
                i+=1

        else:
            if t[17] in ls or t[17]=='X':
                return True
            else:
                return False

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

data.remove('姓名,身份证号码\n')
data1 = [i.strip('\n').split(',') for i in data]
for i in data1:
    if is_valid(i[1]):
        continue
    else:
        data1.remove(i)
data2=sorted(data1,key=lambda x:(x[1][6:13]))
for i in data2:
    nl = 2023 - int(i[1][6:10])
    i.append(nl)
print('姓名,出生日期,年龄')
for a,b,c in data2:
    print('{},{},{}'.format(a,b,c))

 

运行程序截图:

 

10.实验任务10

 

task10-1.py

  1. 程序源代码:
    with open('data10_stu.txt','r',encoding = 'utf-8') as f:
        data = f.readlines()
    
    data1 = [i.strip('\n').split(',') for i in data]
    
    print('{:*^40}'.format('抽点开始'))
    n = int(input('输入随机抽点人数:'))
    import random
    x = list(range(len(data1)))
    t = random.sample(x,n)
    
    for i in t:
        for j in data1[i]:
            print(j)
    
    with open('20230605.txt','w',encoding='utf-8') as f:
        for i in t:
            f.write(','.join(data1[i])+'\n')

     

 

运行程序截图:

 

 

 

 

task10-2.py

程序源代码:

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

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

print('{:*^40}'.format('抽点开始'))
n = int(input('输入随机抽点人数:'))
d= []
while n!=0:
    import random
    x = list(range(len(data1)))
    t = random.sample(x,n)
    for i in t:
        d.append(data[i])
        for j in data1[i]:
            print(j)
    n = int(input('输入随机抽点人数:'))
else:
    print('{:*^40}'.format('抽点结束'))


with open('20230605.txt','w',encoding='utf-8') as f:
    for i in d:
        f.write(''.join(i)+'\n')

 

 

 

 

运行程序截图:

 

 

标签:open,encoding,data1,len,实验,print,data
From: https://www.cnblogs.com/nettj666/p/17458442.html

相关文章

  • 实验6
      task5-1fromturtleimport*importmathdefmove(x,y):penup()goto(x,y)pendown()defmain():x=100*math.sqrt(2)pensize(2)move(-100,-100)begin_fill()forward(200)left(90)forward(200)left(90)......
  • 实验6 turtle绘图与python库应用编程体验
    任务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)mov......
  • 实验5
    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],data2[i]]forii......
  • 实验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)是一种虚拟局域网技术,允许将物理网络划分为逻辑上独立的多个虚拟网......