如何将 python 中的 ascii 字符写入 txt 文件?
How to write to a txt file ascii characters in python?
我正在实施 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 文件中的输出?
感谢您的帮助
您实际上正在获取文本文件中的所有输出。 "problem" 是您的密码正在使用所有 ASCII 字符的全部范围,其中包括一些不可打印的字符。
例如,SOH
是 "start of heading" 符号,在视觉上没有任何实际意义。
我认为您需要使用其他文本编辑器。
Windows'记事本无法正确呈现控制字符。
例如尝试使用 Programmers Notepad 或 Notepad++。
我正在实施 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 文件中的输出?
感谢您的帮助
您实际上正在获取文本文件中的所有输出。 "problem" 是您的密码正在使用所有 ASCII 字符的全部范围,其中包括一些不可打印的字符。
例如,SOH
是 "start of heading" 符号,在视觉上没有任何实际意义。
我认为您需要使用其他文本编辑器。 Windows'记事本无法正确呈现控制字符。
例如尝试使用 Programmers Notepad 或 Notepad++。