假设有一个英文文本文件,编写程序读取其内容,并将其中的大写字母变为小写字母,小写字母变为大写字母。...

方法一:

假设有一个英文文本文件,编写程序读取其内容,并将其中的大写字母变为小写字母,小写字母变为大写字母。
#
先读r,后改,最后写入w f=open('demo.txt','r') s=f.readlines() f.close() r=[i.swapcase() for i in s] #大小写转换 f=open('demo1.txt','w+') f.writelines(r) f.seek(0) ss=f.read() f.close() print('转换结果为:',ss)

方法二:

ls="Just five months on and Ryan Reynolds is back in Beijing. "
print('原始文件为:',ls)
print('转换结果为:',end='')
for i in fn:
    if ord(i)>=65 and ord(i)<=90 :
        print(i.lower(),end='')
    elif ord(i)>=97 and ord(i)<=122:
        print(i.upper(),end='')
    else:
        print(i,end='')

方法三:

ls="Just five months on and Ryan Reynolds is back in Beijing. "
print('原始文件为:',ls)
print('转换结果为:',end='')
res=''
for i in ls:
    if i.islower():
        res+=i.upper()
    elif i.isupper():
        res+=i.lower()
    else:
        res+=i
print(res)

方法四:

def uptolow(filepath):
    res=''
    with open(filepath,'r') as f:
        ss=f.readlines()
        for s in ss:           
            for i in s:
                if i.islower():
                    res+=i.upper()
                elif i.isupper():
                    res+=i.lower()
                else:
                    res+=i
        return res

if __name__ =="__main__":
    filepath='demo.txt'
    print(uptolow(filepath))

 

转载于:https://www.cnblogs.com/huigebj/p/11425294.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值