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
これを修正する方法についてのヘルプは大歓迎です! ありがとう!