汉堡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 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题