首页 > 编程语言 >实验5 文件应用编程

实验5 文件应用编程

时间:2023-06-06 19:23:47浏览次数:43  
标签:文件 old encoding open 编程 实验 print new data

task6

源代码:

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

new_data = []
for i in range(len(old_data)):
    if eval(old_data[i]) + float(0.5) >= int(eval(old_data[i])) + 1:
        new_data.append(str(int(eval(old_data[i]))+1))
    else:
        new_data.append(str(int(eval(old_data[i]))))

title = ['原始数据', '四舍五入后数据']
data = []
for i in range(len(old_data)):
    data.append([old_data[i], new_data[i]])

with open('data6.csv', 'w', encoding='gbk') as f:
    f.write(','.join(title) + '\n')
    for i in data:
        f.write(','.join(i) + '\n')
print('原始数据:')
print(old_data)
print('四舍五入后数据:')
print(new_data)

运行截图:

 

task7

源代码:

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='utf-8') 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]))

运行截图:

 

task8

源代码:

with open('hamlet.txt', 'r', encoding='utf-8')as f:
    data = f.readlines()
    print('hamlet.txt粗略统计:')
    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

源代码:

print('姓名,出生日期,年龄')
for i in data:
    lst = i.split(',')
    data2.append(lst)
lst2 = []
for i in data2:
    if is_valid(i[1]) == True:
        name = i[0]
        btd = i[1][6:14]
        age = str(2023 - int(i[1][6:10]))
        lst2.append([name, btd, age])
lst2.sort(key=lambda x: x[2], reverse=True)
for i in lst2:
    print(i[0], end=',')
    print(f'{i[1][:4]}-{i[1][4:6]}-{i[1][6:8]}', end=',')
    print(i[2])

运行截图:

 

task10_1

源代码:

with open('data10_stu.txt','r',encoding='utf-8') as f:
    data = f.readlines()
print('{:*^40}'.format('抽点开始'))
n = int(input('输入随机抽点人数:'))
import random
x = []
x2 = ''
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)
sum1 = 0
for i in range(n):
    print(data[x[i]])
    x2 += data[x[i]]
    sum1 += n
with open('20230602.txt', 'w', encoding='utf-8') as f:
    f.writelines(x2)

运行截图;

 

task10_2

源代码:

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('20230601.txt', 'w', encoding='utf-8') as f:
    f.writelines(x2)

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

运行截图:

 

标签:文件,old,encoding,open,编程,实验,print,new,data
From: https://www.cnblogs.com/Hj-soo320/p/17461476.html

相关文章

  • 实验五
    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......
  • docker 操作nginx命令+docker-compose常用命令及yml文件编写
    docker-compose常用命令及yml文件编写https://blog.csdn.net/doubiy/article/details/118997661 https://docs.docker.com/compose/1.观察下载容器镜像过程dockerrun-dnginx:latest-d表示当前终端的后台运行nginx:latest就是最新的nginx版本2.访问容器中的ngi......
  • Idea修改html文件不需重启
    1.首先设置为自动编译“File”->“Settings”->“Build,Execution,Deplyment”->“Compiler”2.允许运行时自动编译“Shift+Ctrl+Alt+/”,选择“Registry”,选中打勾“compiler.automake.allow.when.app.running”......
  • 实验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......
  • Linux 创建用户、赋予用户权限、修改文件夹权限、修改只读文件
    创建用户#useraddwork设置用户密码#passwdwork赋予用户权限方法一:修改/etc/sudoers文件,找到下面一行,把前面的注释(#)去掉##Allowspeopleingroupwheeltorunallcommands%wheelALL=(ALL)ALL然后修改用户,使其属于root组(wheel),命令如下:#usermod-grootwork......