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

    方法一:

    假设有一个英文文本文件,编写程序读取其内容,并将其中的大写字母变为小写字母,小写字母变为大写字母。
    #
    先读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))
  • 相关阅读:
    结对作业评分
    Week6&7——第一次项目冲刺(Alpha版本)
    软工辅修团队项目选题参考
    Week5——团队选题&需求分析
    Week4——结对练习&团队作业1
    nginx与Apache的对比以及优缺点
    oracle数据库-错误编码大全
    spring MVC原理
    Java操作Memcached
    zookeeper应用场景-java
  • 原文地址:https://www.cnblogs.com/huigebj/p/11425294.html
Copyright © 2011-2022 走看看