我已经编写了代码来编码"编码"使用ord功能将歌词粘贴到文本文件中。这是下面的代码:
import os
filename = os.path.abspath("WeWillRockYou.txt")
out_file = open('WeWillRockYou2.txt', 'w')
readFile = open (filename, 'r')
for line in readFile:
for char in line:
if not char == "\n":
out_file.write(str(ord(char)))
else:
out_file.write(char)
out_file.close()
之后,这些歌词被放入一个新的文本文件,但是作为ASCII。现在我正在尝试制作一个能够解码的代码。这首歌的歌词并将它们写入原来的新文本文件中,但是我收到了一个错误。下面的解码代码:
import os
filename = os.path.abspath("WeWillRockYou2.txt")
out_file = open('WeWillRockYou3.txt', 'w')
readFile = open (filename, 'r')
for line in readFile:
for num in line:
if not num == "\n":
out_file.write(int(chr(num)))
else:
out_file.write(char)
out_file.close()
但我收到错误:
Traceback (most recent call last):
line 16, in <module>
out_file.write(int(chr(num)))
TypeError: an integer is required
如何解决这个问题的任何帮助将不胜感激! Thankss!