首页 > 试题广场 >

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

[问答题]

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

f = open(r'd:\1.txt','r')
s = f.readlines()
f.close()
 
r = [i.swapcase() for i in s]
 
f = open(r'd:\2.txt','w')
f.writelines(r)
f.close()

发表于 2017-12-28 15:56:16 回复(0)
def up22low(filepath):
    res = ''
    with open(filepath, 'r') as f:
        contents = f.readlines()
        for content in contents:
            for i in content:
                if i.islower():
                    res += i.upper()
                elif i.isupper():
                    res += i.lower()
                else:
                    res += i
    return res
            
if __name__ == '__main__':
    filepath = 'h.txt'
    print(up22low(filepath))

发表于 2018-05-04 20:53:37 回复(0)