SDUT-python实验七 主观题+编程题

主观题

8-1 文件编程7-1        分数 10

有一个英文文件"example.txt".编写一个程序把大写字母变小写,小写字母变大写,其他字符不变. 结果写入文件"result.txt"。@
程序压缩后(zip)以文件形式上传!

f1=open('example.txt','r')

f2=open('result.txt','w')

s=""

for i in f1.read():

    if i.isupper():

        s+=i.lower()

        continue

    elif i.lower():

        s+=i.upper()

        continue

    else:

        s+=i

f2.write(s)

f1.close()

f2.close()

8-2 文件编程7-2        分数 10

统计文本文件"letter.txt"中各类字符个数:分别统计字母( 大小写不区分),数字及其他字符的个数。@
程序压缩后(zip)以文件形式上传!

f1=open('letter.txt','r')

n1,n2,n3=0,0,0

for i in f1.read():

    if i.isalpha():

        n1+=1

    elif i.isdigit():

        n2+=1

    else:

        n3+=1

print("字母:%d 数字:%d 其他字符:%d "%(n1,n2,n3))

f1.close()

8-3 文件编程7-3        分数 10

马丁路德金的"I have a dream"节选存放在"freedom.txt"中:

I have a dream that one day this nation will rise up, live up to the true meaning of its creed: “We hold these truths to be self-evident; that all men are created equal.”

I have a dream that one day on the red hills of Georgia the sons of former slaves and the sons of former slave-owners will be able to sit down together at the table of br otherhood.

I have a dream that one day even the state of Mississippi, a state sweltering with th e heat of injustice, sweltering with the heat of oppression, will be transformed into an oasis of freedom and justice.

I have a dream that my four children will one day live in a nation where they will no t be judged by the color if their skin but by the content of their character. I have a dream today.

I have a dream that one day down in Alabama with its governor having his lips drippin g with the words of interposition and nullification, one day right down in Alabama li ttle black boys and black girls will be able to join hands with little white boys and white girls as sisters and brothers.

I have a dream today.

I have a dream that one day every valley shall be exalted, every hill and mountain sh all be made low, the rough places will be made plain, and the crooked places will be made straight, and the glory of the Lord shall be revealed, and all flesh shall see i t together.

编程实现词汇表,计算每一个单词出现的次数,大小写不区分,输出到"dic.txt" 文件保存。
程序压缩后(zip)以文件形式上传! 

import collections

f1=open("freedom.txt",'r',encoding='utf-8')

f2=open("dic.txt","w")

s=''.join([i.lower() if i.isalpha() else i for i in f1.read()])

s=''.join(["" if not i.isalpha() else i for i in s])

s=s.split()

c=dict(sorted(collections.Counter(s).items(),key=lambda x:x[1],reverse=True))

f2.write("单词,单词个数\n")

for i,j in c.items():

    s=i+','+str(j)+'\n'

    f2.write(s)

f1.close()

f2.close()

8-4 文件编程7-4        分数 10

用水量文件"water.txt"的第一列为账号,下面是每个月的用水量(后一个数-前一个数),共十二个月。每立方米需付1.05元。编程计算每户一年的水费,结果保存在fee.txt文件中。程序和结果文件压缩后(zip格式)以文件形式上传!

water.txt格式如下:
0000359333 772 789 806 847 880 901 950 991 1022 1043 1064 1089 1114
0000359305 121 132 145 156 168 179 192 206 219 230 246 258 273
0000359708 1008 1046 1102 1167 1209 1255 1311 1362 1407 1453 1512 1563 1604
0000359504 541 567 590 622 651 689 701 732 758 775 796 814 847
0000359209 401 412 441 466 479 490 508 522 541 572 603 637 666

fee.txt格式如下(保留2位小数):
0000359333 359.10
0000359305 159.60
0000359708 625.80
0000359504 321.30
0000359209 278.25

f1=open('water.txt','r')

f2=open('fee.txt','w')

for i in f1.readlines():

    i=i.split()

    pay=float('%0.3f'%((int(i[-1])-int(i[1]))*1.05))

    f2.write(i[0]+' ')

    f2.write('%.2f\n'%pay)

f1.close()

f2.close()

 编程题

9-1 求文件行数

下载题目附件,编辑src/目录下的test.py文件,实现读取统计data.txt文件的有效行数,
并将结果输出保存到result.txt文件。(20分) **

**说明: **

(1)有效行指至少包括一个字符行,空行不计为有效行

(2)程序文件名 test.py 不能修改

(3)本地编写测试完成后,将src文件夹打包为 src.zip文件后上传提交

data.txt的内容如下:

python程序设计

人生苦短,我学python
程序设计

抽象过程
自动化求解的计算思维
结合问题思考程序结构

输出结果如下:结果写入到result.txt中

有效行数为:6行
'''
data.txt中保存若干行文本。
请编写一个程序读取文件中文本,并统计输出文本的有效行数,
然后将结果保存到result.txt中。

程序代码必须保存到test.py中

输出格式:
有效行数为:6行
'''
f1=open("data.txt","r",encoding="utf-8")
f2=open("result.txt","w",encoding="utf-8")

s=f1.readlines()
d=[x.strip() for x in s if len(x.strip())>0]
f2.write("有效行数为:{}行".format(len(d)))
f2.close()

  • 5
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值