【问题标题】:How to write to a txt file ascii characters in python?如何在python中写入txt文件ascii字符?
【发布时间】:2016-07-07 00:50:39
【问题描述】:

我正在实现一个 XOR 方法,我想将加密的消息写入一个 txt 文件,但是在我这样做的方式中,我得到了奇怪的字符而不是消息。

代码如下:

from itertools import cycle

msg = 'RUNNINGFAST'
key = 'SADSTORY'

cipher = ''.join(chr(ord(c)^ord(k)) for c,k in zip(msg, cycle(key)))

print('%s ^ %s = %s ' % (msg, key, cipher))
msg = ''.join(chr(ord(c)^ord(k)) for c,k in zip(cipher, cycle(key)))
print('%s ^ %s = %s ' % (cipher, key, msg))

with open("XOR - Msg_Cipher.txt", "w",) as text_file:
    text_file.write("msg: %s \nCipher: %s" % (msg, cipher))

输出如下所示:

txt 文件如下所示:

如何获取txt文件中的输出?

感谢您的帮助

【问题讨论】:

  • 那个的输出。它主要由不可打印的字符组成。

标签: python ascii writetofile


【解决方案1】:

您实际上正在获取文本文件中的所有输出。 “问题”是您的密码正在使用所有 ASCII 字符的全部范围,其中包括一些不可打印的字符。

例如,SOH 是“标题的开始”符号,在视觉上并没有任何意义。

ASCII chart

【讨论】:

    【解决方案2】:

    我认为您需要使用另一个文本编辑器。 Windows 的记事本无法正确呈现控制字符。

    例如尝试使用 Programmers Notepad 或 Notepad++。

    【讨论】:

    • 谢谢,就是这样,当我用记事本++打开txt文件时,我得到了所有的输出。
    猜你喜欢
    • 2014-04-04
    • 2014-04-05
    • 2013-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-02
    • 1970-01-01
    相关资源
    最近更新 更多