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


    方法一:

    假设有一个英文文本文件,编写程序读取其内容,并将其中的大写字母变为小写字母,小写字母变为大写字母。
    #
    先读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))
  • 相关阅读:
    京东精益敏捷教练分享:敏捷助力产品创新!
    设计规范 | 详解组件控件结构体系
    Axure响应式进阶
    通讯录表设计
    TEST1
    c#练习四单元总结
    窗体控件应用总结(1)
    .NET(c#理解)
    test2-11
    test1-1
  • 原文地址:https://www.cnblogs.com/huigebj/p/11425294.html
Copyright © 2020-2023  润新知