首页 > 其他分享 >实验5

实验5

时间:2023-06-06 13:33:06浏览次数:35  
标签: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)

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/fyx1978501071/p/17460306.html

相关文章

  • 实验6 turtle绘图和Python库应用编程体验
    实验任务1task1_1.py源代码1fromturtleimport*23defmove(x,y):4penup()5goto(x,y)6pendown()78defdraw(n,size=100):9foriinrange(n):10fd(size)11left(360/n)1213defmain():14pensize(2)1......
  • 实验5
     with open('data6.csv','r',encoding='gbk') as f:  data1 = f.read().split('\n')del data1[0]print('原始数据:')print(data1)for i in range(len(data1)):    data1[i] = float(data1[i])data2 = []for i in ra......
  • 实验6 turtle绘图与python库应用编程体验
    实验任务1task1_11fromturtleimport*23defmoveto(x,y):4'''5画笔移动到坐标(x,y)处6'''7penup()8goto(x,y)9pendown()1011defdraw(n,size=100):12'''13绘制边长为s......
  • 实验五 文件应用编程
    实验任务61#读取并处理原始数据withopen('data6.csv','r',encoding='gbk')asf:2old_data=f.read().split('\n')3delold_data[0]4#四舍五入得到新数据5new_data=[]6foriinrange(len(old_data)):7ifeval(ol......
  • 实验5 文件应用编程
    实验任务61withopen('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)......
  • 实验5
    task6.py源代码:1withopen('data6.csv','r',encoding='gbk')asf:2data1=f.read().split('\n')3deldata1[0]4print('原始数据:')5print(data1)67foriinrange(len(data1)):8data1[i]......
  • 实验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)......
  • 实验6
    1、实验任务1task1_1.py实验源码:1fromturtleimport*23defmove(x,y):4penup()5goto(x,y)6pendown()78defdraw(n,size=100):9foriinrange(n):10fd(size)11left(360/n)1213defmain():14pens......
  • 实验5 文件应用编程
    task6.py1withopen('data6.csv','r',encoding='gbk')asf:2data=list(f.readlines())3data=[i.strip('\n')foriindata]45importdecimal6decimal.getcontext().rounding='ROUND_HALF_UP'7......
  • 实验6 turtle绘图与python库应用编程体验
    实验任务1:使用turtle绘制基础图形task1_1.py实验源码:1fromturtleimport*23defmove(x,y):4penup()5goto(x,y)6pendown()78defdraw(n,size=100):9foriinrange(n):10fd(size)11left(360/n)12......