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

实验5 文件应用编程

时间:2023-06-06 22:46:17浏览次数:34  
标签:文件 open encoding data 编程 len 实验 print data1

task6

源代码:

with open('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')

  

运行结果:

 

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

源代码:

lines = 0
words = 0
sum1 = 0
space = 0
with open('hamlet.txt','r',encoding='utf-8') as f:
    for line in f:
        word = line.split()
        lines += 1
        words += len(word)
        sum1 += len(line)
        for i in line:
            if i == ' ':
                space += 1
            else:
                pass
print('hamlet.txt粗滤统计:')
print(f'行数:{lines}')
print(f'单词数:{words}')
print(f'字符数:{sum1}')
print(f'空格数:{space}')

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

for i in range(len(text)):
    text[i] = str(i+1) + ' ' + text[i]

with open('hamlet_with_line_number.txt','w',encoding = 'utf-8') as f:
    f.writelines(text)

  

运行结果:

task9

源代码:

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


with open('data9_id.txt','r',encoding='utf-8') as f:
    data = f.read().split('\n')
del data[0]
data2 = []
data3 =[]
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('20230606.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('20230606.txt','w',encoding='utf-8') as f:
    f.writelines(x2)

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

  

运行结果:

 

标签:文件,open,encoding,data,编程,len,实验,print,data1
From: https://www.cnblogs.com/3100974046y/p/17461944.html

相关文章

  • IDEA创建文件夹的巨坑
    今天学习 073-角色管理-删除角色-后端逻辑处理_哔哩哔哩_bilibili 时使用到了mybatis-plush但是启动服务的时候发现控制台打印了:Registeredplugin:'com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor@705d2918'Property'mapperLocations'wasnotspe......
  • 通过状态机方法实现基于FPGA的维特比译码器,包含testbench测试文件
    1.算法仿真效果vivado2019.2仿真结果如下: 2.算法涉及理论知识概要       viterbi译码算法是一种卷积码的解码算法。优点不说了。缺点就是随着约束长度的增加算法的复杂度增加很快。约束长度N为7时要比较的路径就有64条,为8时路径变为128条。(2<<(N-1))。所以viterbi......
  • MyBatis学习笔记(4)—— XML映射文件の结果集(Result Maps)高级结果映射中的集合(collectio
    集合<collectionproperty="posts"ofType="domain.blog.Post"><idproperty="id"column="post_id"/><resultproperty="subject"column="post_subject"/><resultproperty="......
  • Spring 学习笔记(7)—— 使用外部属性文件
    1使用PropertyPlaceholderConfigurer属性文件驱动器类名,JDBC的URL地址及数据库用户名/密码都直接写在XML文件中,部署应用的时候,必须先找出这个Bean部署XML文件,再找出数据源Bean定义的代码段进行调整,很麻烦。在实际应用中,应该将这些配置信息抽取到一个配置文件中。2使......
  • Spring 学习笔记(5)—— 简化xml配置文件的配置方式
    1字面值属性2引用对象属性3使用p命名空间......
  • 实验5 文件应用编程
    实验任务61withopen('data6.csv','r',encoding='gbk')asf:2data1=f.read().split('\n')3deldata1[0]4foriinrange(len(data1)):5data1[i]=eval(data1[i])6data1[i]=float(data1[i])7pri......
  • RDS 、HDFS、 mapreduce 、spark 、hive、 hbase 、zookeeper 、kafka 、flume、mysql
    这些技术是大数据领域的常用组件,它们之间的配置文件依赖关系如下:RDS是一种关系型数据库,可以独立安装和使用,不需要依赖其他组件。HDFS是Hadoop分布式文件系统,通常与MapReduce一起使用。在Hadoop集群中,HDFS需要配置core-site.xml和hdfs-site.xml两个文件,其中core-site......
  • C语言编程语法—文件读写
    一个文件,无论它是文本文件还是二进制文件,都是代表了一系列的字节。C语言不仅提供了访问顶层的函数,也提供了底层(OS)调用来处理存储设备上的文件。本章将讲解文件管理的重要调用。打开文件您可以使用fopen()函数来创建一个新的文件或者打开一个已有的文件,这个调用会初始化类型FIL......
  • 实验6 turtle绘图与python库应用编程体验
    实验任务1task1_1fromturtleimport*defmoveto(x,y):'''画笔移动到坐标(x,y)处'''penup()goto(x,y)pendown()defdraw(n,size=100):'''绘制边长为size的正n边形'''for......
  • 实验5
    1withopen('data6.csv','r',encoding='gbk')asf:2data1=f.read().split('\n')3deldata1[0]4print('原始数据:')5print(data1)67foriinrange(len(data1)):8data1[i]=float(data1[i]......