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

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

作者: 寒雨墨轩 | 来源:发表于2019-11-07 16:08 被阅读0次

    #第一种方法

    with open("1_English.txt", 'r+') as f:

        s = f.read()

        print(s)

        ss = [i.swapcase() for i in s]

        f.seek(0)

        f.writelines(ss)


    #第二种方法

    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.haomeiwen.com/subject/bzevbctx.html