python实验七、文件处理

实验七、文件处理

续上实现四,以下内容是第五次实验

1、统计字母(Pyprog0701.py)

题目描述:编马程序读出当前目录下 Pyprog0701.bt 文件中所有内容,然后将文件中的所有字母转换为小写字母。输入2个小写字母ch1和ch2,统计文件中字符在ch1 和 ch2 两字母之间的字母数。
在这里插入图片描述
输入格式:在第一行中输入一个小写字母ch1,在第二行输入另一个小写字母ch2。
输出格式:在一行中按“The number of characters between * and # in file is XX”格式输出统计结果,其中“*”是一个小写字母,表示统计的起始字母,“#”也是一个小写字母,表示统计的结束字母,“×X”是一个整数,表示两字母之间的字母数。
输入输出样例1
在这里插入图片描述

import sys,os,shutil
if os.path.exists('Pyprog0701.in') and os.path.isfile('Pyprog0701.in'):
    sys.stdin=open('Pyprog0701.in', 'r')
    sys.stdout=open('Pyprog0701.out', 'w')
    sys.stderr=open('Pyprog0701.err', 'w')
    oldfile0701=os.path.join(os.path.abspath(os.path.dirname(os.getcwd())), 'data\Pyprog0701.txt')
    newfile0701=os.path.join(os.getcwd(),'Pyprog0701.txt')
    if os.path.exists(oldfile0701): shutil.copyfile(oldfile0701, newfile0701)
###不得删改移动上述代码,请在下一行开始按题目要求编写代码,否则不得分###

with open("Pyprog0701.txt") as file:
    content = file.read()

content = content.lower()

ch1 = input()
ch2 = input()

if ch1 > ch2:
    ch1,ch2 = ch2,ch1

count = 0 
for char in content:
    if ch1 <= char <= ch2:
        count += 1
with open("'Pyprog0701.out","w") as output_file:
    output_file.write(f"The number of characters between {ch1} and {ch2} in file is {count}.")

sys.stdout.close()
sys.stdin.close()

2、整除数据(pyprog0702.py)

题目描述:已知 Pyprog0702.csv 文件中有20个正整数,各项数据均使用英文逗号分隔。编写程序读出当前目录下 Pyprog0702.csv文件中20 个正整数,依次存入整数心列表 nList,然后输入一个整数n,按列表正序序号的顺序输出 nList 中能被n 整除的整数。
在这里插入图片描述
输入格式:在一行中输入一个正整数n(1≤n≤9)。
输出格式:在一行中输出 nList 列表中能被 n 整除的整数,整数之间用一个空格隔开。
在这里插入图片描述

import sys,os,shutil
if os.path.exists('Pyprog0702.in') and os.path.isfile('Pyprog0702.in'):
    sys.stdin=open('Pyprog0702.in', 'r')
    sys.stdout=open('Pyprog0702.out', 'w')
    sys.stderr=open('Pyprog0702.err', 'w')
    oldfile0702=os.path.join(os.path.abspath(os.path.dirname(os.getcwd())), 'data\Pyprog0702.csv')
    newfile0702=os.path.join(os.getcwd(),'Pyprog0702.csv')
    if os.path.exists(oldfile0702): shutil.copyfile(oldfile0702, newfile0702)
###不得删改移动上述代码,请在下一行开始按题目要求编写代码,否则不得分###
with open("Pyprog0702.csv","r") as file:
    data = file.read()
    
nlist = [int(num) for num in data.split(",")]

n = int(input())

result = [str (num) for num in nlist if num % n == 0]

print(" ".join(result))

    

3、统计成绩(Pyprog0703.py)

题目描述:已知 Pyprog0703.csv 文件中存放10名考生4门课程成绩,各项数据均使用英文逗号分隔。编写程序读出当前目录下 Pyprog0703.csv 文件中内容,统计每个考生的4门课程的总分,然后输入2个正整数 LScore 和 UScore,按文件中考生顺序依次分行输出总分位于[LScore,UScore]区间的考生准考证号和总分。
在这里插入图片描述

输入格式:在第一行中输入一个整数 LScore,在第二行再输入另一个整数 UScore,200<=LScore <UScore<= 400.
输出格式:每行按格式“准考证号:******** 总分:XXX”,输出符合条件的考生信息,其中“********”为考生的准考证号,“XXX”为总分,“”为英文冒号,总分和准考证之间用一个空格隔开。
在这里插入图片描述

import sys,os,shutil
if os.path.exists('Pyprog0703.in') and os.path.isfile('Pyprog0703.in'):
    sys.stdin=open('Pyprog0703.in', 'r')
    sys.stdout=open('Pyprog0703.out', 'w')
    sys.stderr=open('Pyprog0703.err', 'w')
    oldfile0703=os.path.join(os.path.abspath(os.path.dirname(os.getcwd())), 'data\Pyprog0703.csv')
    newfile0703=os.path.join(os.getcwd(),'Pyprog0703.csv')
    if os.path.exists(oldfile0703): shutil.copyfile(oldfile0703, newfile0703)
###不得删改移动上述代码,请在下一行开始按题目要求编写代码,否则不得分###
with open("Pyprog0703.csv","r")as file:
    lines = file.readlines()
    
scores = {}

for line in lines:
    data = line.strip().split(',')
    if len(data) == 5:
        student_id = data[0]
        total_score = sum(int(score) for score in data[1:] if score.isdigit())
        scores[student_id] = total_score
        
LScore = int(input())
UScore = int(input())

for student_id,total_score in scores.items():
    if LScore <= total_score <= UScore:
        print(f"准考证号:{student_id} 总分:{total_score}")

4、创建通讯录(Pyprog0704.py)

题目描述:输入2个联系人的姓名和电话,创建一个通讯录文件 Pyprog0704.csv,将输入的 n个联系人信息,以“姓名,联系电话”(使用英文逗号分隔》格式分行存入
文件中。
输入格式:在第一行中输入一个整数n,在随后的n行中,每行输入两个字符串姓名和电话号码,这两个字符串之间用一个空格隔开。
输出格式:Pyprog0704.csv 文件。
在这里插入图片描述

import sys,os
if os.path.exists('Pyprog0704.in') and os.path.isfile('Pyprog0704.in'):
    sys.stdin=open('Pyprog0704.in', 'r')
    sys.stdout=open('Pyprog0704.out', 'w')
    sys.stderr=open('Pyprog0704.err', 'w')
    if os.path.exists('Pyprog0704.csv'): os.remove('Pyprog0704.csv')
###请在此两行之间按题目要求编写代码,不得移动删改上述代码,否则不得分############################

n - int(input())
with open("Pyprog0704.csv","w") as file:
    file.write("姓名,联系人\n")
    for i in range(n):
        info = input().split()
        name,phone = info[0],info[1]
        file.write(f"{name},{phone}\n")

###请在此两行之间按题目要求编写代码,不得删改下面代码,否则不得分############################
if os.path.exists('Pyprog0704.in') and os.path.isfile('Pyprog0704.in'):
    if os.path.exists('Pyprog0704.csv'):
        filename0704=open('Pyprog0704.csv', 'r')
        print(filename0704.read())
        filename0704.close()

5、统计文本词频(Pyprog0705.py)

题目描述:对输入一段英文文章(包含字符“,”和“.”),编写程序统计该段文章中的每个单词出现的次数(以小写为准),然后创建一个通讯录文Pyprog0705.csv,将排名前3个高频词语,以“单词.次数”《使用英文逗号分隔)格式分行存入Pyprog0705.csy 文件中。
输入格式:在一行中输入一个段英文文章,单词之间用一个空格隔开。
输出格式:Pyprog0705.csv 文件
在这里插入图片描述

import sys,os
if os.path.exists('Pyprog0705.in') and os.path.isfile('Pyprog0705.in'):
    sys.stdin=open('Pyprog0705.in', 'r')
    sys.stdout=open('Pyprog0705.out', 'w')
    sys.stderr=open('Pyprog0705.err', 'w')
    if os.path.exists('Pyprog0705.csv'): os.remove('Pyprog0705.csv')
###请在此两行之间按题目要求编写代码,不得移动删改上述代码,否则不得分########################
import csv
from collections import Counter
import re

a = input()

w_s = re.findall(r'\w+',a.lower())

w_c = Counter(w_s)

t_w = w_c.most_common(3)

with open("Pyprog0705.csv","w",newline='') as file:
    csv_writer = csv.writer(file)
    csv_writer.writerow(["单词","次数"])

    for w,c in t_w:
        csv_writer.writerow([w,c])
###请在此两行之间按题目要求编写代码,不得删改下面代码,否则不得分############################
if os.path.exists('Pyprog0705.in') and os.path.isfile('Pyprog0705.in'):
    if os.path.exists('Pyprog0705.csv'):
        filename0705=open('Pyprog0705.csv', 'r')
        print(filename0705.read())
        filename0705.close()

  • 7
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

-Z_Nuyoah

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值