汉堡361 2022-06-12 22:54 采纳率: 100%
浏览 24
已结题

加密,保存在新的文件里

建立一个文件(c.txt),将以下内容存入到文件中。然后将该文件中的每个英文字母加密后写入到一个新文件(d.txt)。加密的方法是:将A变成B,B变成C,……,Y变成Z,Z变成A;a变成b,b变成c,……,y变成z,z变成a,其他字符不变化。
I say to you today, my friends.
And so even though we face the difficulties of today and tomorrow, I still have a dream. It is a dream deeply rooted in the American dream.

  • 写回答

1条回答 默认 最新

  • vigiles 2022-06-13 01:38
    关注
    with  open("c.txt", "w") as file:  # w只写方式。如果文件不存在,则新建;存在,则覆盖;a追加方式。如果文件不存在,则新建;存在,则打开继续操作
        file.write("I say to you today, my friends.")  # 写入数据
        file.write("And so even though we face the difficulties of today and tomorrow, I still have a dream. It is a dream deeply rooted in the American dream.") 
    
    with open("d.txt", "w") as fd:
        with open("c.txt") as fc: # 默认“r”
            line = fc.readline()
            words = []
            for letter in line: # 遍历字符
                ascii = ord(letter) # 字符转ascii
                tni = int(ascii) # 小写a97-z122,大写A65-Z90
                if tni >= 97 and tni < 122:
                    tni += 1
                elif tni == 122:
                    tni = 97
                elif tni >= 65 and tni < 90:
                    tni += 1
                elif tni == 90:
                    tni = 65
                char = chr(tni) # ascii转字符
                words.append(char)
            encrypted = "".join(words)
            fd.write(encrypted)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 6月24日
  • 已采纳回答 6月16日
  • 创建了问题 6月12日

悬赏问题

  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等