方法一:

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

 

相关文章:

  • 2021-11-23
  • 2022-12-23
  • 2022-12-23
  • 2022-02-09
  • 2022-01-01
  • 2021-09-16
  • 2022-02-06
猜你喜欢
  • 2022-12-23
  • 2021-10-26
  • 2022-12-23
  • 2021-12-03
  • 2022-12-23
  • 2021-11-23
  • 2022-12-23
相关资源
相似解决方案