首页 > 其他 > 详细

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

时间:2019-08-28 17:54:36      阅读:660      评论:0      收藏:0      [点我收藏+]

方法一:

假设有一个英文文本文件,编写程序读取其内容,并将其中的大写字母变为小写字母,小写字母变为大写字母。
#
先读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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!