首页 > 其他分享 >实验5

实验5

时间:2023-06-06 19:48:03浏览次数:34  
标签:截图 title open 实验 print data1

6.实验任务6

task6.py

实验代码:

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 range(len(data1)):
    data2.append(int(data1[i]+0.5))
print('四舍五入后数据:')
print(data2)

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

实验结果截图:

 

实验文件截图:

 

7.实验任务7

task7.py

实验代码:

with open('data7.csv','r',encoding='gbk') as f:
    data1 = f.read().split('\n')
del data1[0]

lsta = []
lstm = []
for i in data1:
    data1.sort(key = lambda i :(i[-2],i[-1]),reverse = True)
for i in data1:
    lst1 = i.split(',')
    if lst1[2] == 'Acting':
        lsta.append(lst1)
    else:
        lstm.append(lst1)

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

 

实验结果截图:

 

实验文件截图:

 

8.实验任务8

task8.py

实验代码:

with open('hamlet.txt','r') as f:
    data = f.read()
print('hamlet.txt粗略统计:')
data1 = data.splitlines()
print(f'行数:{len(data1)}')
print(f'单词数:{len(data.split())}')
print(f'字符数:{len(data)}')
k = data.count(' ')
print(f'空格数:{k}')

data2=[]
for i in range(len(data1)):
    data2.append(f'{i+1} '+ data1[i] + '\n')
with open('hamlet_with_line_number.txt','w') as f:
    f.writelines(data2)

实验结果截图:

 

 实验文件截图:

 

9.实验任务9

task9.py

实验代码:

import datetime


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


with open('data9_id.txt', 'r', encoding='utf-8') as f:
    data0 = f.readlines()
    title = data0.pop(0)

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

data1 = []
for i in data0:
    if is_valid(i[1]):
        data1.append(i)
print(data1)

t = datetime.datetime.now()
y = t.strftime('%Y%m%d')
for i in data1:
    age = str(int(y)-int(i[1][6:14]))
    i.append(age[:2])
    i[1] = i[1][6:10] + '-' + i[1][10:12] + '-' + i[1][12:14]
data2 = sorted(data1, key=lambda x: -int(x[2]))

print('姓名, 出生日期, 年龄')
for i in data2:
    print(', '.join(i))

实验结果截图:

 

 

10.实验任务10

task10_1.py

实验代码:

import random
import datetime

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

data0 = [i.strip('\n').split('\t') for i in data]
n = eval(input('输入随机抽点人数:'))
data1 = random.sample(data0, n)

for i in data1:
    print('  '.join(i))

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

with open(f'{title}.txt', 'w', encoding='utf-8') as f:
    for i in data1:
        f.write(f'{i[0]}\t{i[1]:6s}\t{i[2]:10s}\n')

 

实验结果截图:

 

实验文件截图:

 

task10_2.py

实验代码:

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

with open('data10_stu.txt', 'r', encoding='utf-8') as f:
    data = f.readlines()
data0 = [i.strip('\n').split('\t') for i in data]

print(f'{"抽点开始":*^40}')
n = eval(input('输入随机抽点人数:'))

a = 0
while n != 0:
    data1 = random.sample(data0, n)
    a += 1
    for i in data1:
        print('  '.join(i))
        data0.remove(i)
    if a == 1:
        with open(f'{title}.txt', 'w', encoding='utf-8') as f:
            for i in data1:
                f.write(f'{i[0]}\t{i[1]:6s}\t{i[2]:10s}\n')
    else:
        with open(f'{title}.txt', 'a', encoding='utf-8') as f:
            for i in data1:
                f.write(f'{i[0]}\t{i[1]:6s}\t{i[2]:10s}\n')
    n = eval(input('输入随机抽点人数:'))
print(f'{"抽点结束":*^40}')

 

实验结果截图:

 

实验文件截图:

 

标签:截图,title,open,实验,print,data1
From: https://www.cnblogs.com/xjw88/p/17461518.html

相关文章

  • 实验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[......
  • 实验5 文件应用编程
    task6源代码 withopen('data6.csv','r',encoding='gbk')asf:data=f.readlines()data.pop(0)data=[eval(i.strip())foriindata]print(f'原始数据:\n{data}')processed_data=[int(i+0.5)foriindata]......
  • 实验5 文件应用编程
    实验5文件应用编程实验任务6: task6.py1withopen('data6.csv','r',encoding='gbk')asf:2old_data=f.read().split('\n')3delold_data[0]4processed_data=[]5foriinrange(len(old_data)):6ifeval(ol......
  • 实验5 文件应用编程
    task6源代码:withopen('data6.csv','r',encoding='gbk')asf:old_data=f.read().split('\n')delold_data[0]new_data=[]foriinrange(len(old_data)):ifeval(old_data[i])+float(0.5)>=int(eval(o......
  • 实验五
    task6源代码:withopen('data6.csv','r',encoding='gbk')asf:data=f.readlines()data.remove('原始数据\n')data1=[]foriindata:ifi[-1]=='\n':i=i[:-1]data1.append(float(i))print(&#......
  • 实验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......