我正在尝试编写一个打开文件的程序,读取文件然后将文件转换为ASCII字符,然后将这些字符写入输出文件。但是,当我运行该程序时,我得到< _io.TextIOWrapper name ='Output.txt'mode ='r'noding ='cp1252'>
def convert_to_ascii(file,Output):
F = open(file, 'r')
B = open(Output, 'w')
F_line=str(F.readline())
for _ in F:
B.write([ord(str(F_line[i])) for i in F_line])
F.close()
B.close()
file = input('what file do you wish to convert?')
convert_to_ascii(file,'Output.txt')
p=open('Output.txt', 'r')
print(p)
答案 0 :(得分:0)
你可以这样试试,
file_content = open(file, 'r').read()
file_content = ''.join(str(ord(c)) for c in file_content))
open('Output.txt','w').write(file_content)
答案 1 :(得分:0)
我不明白为什么要打印输出文件。尝试这样的事情:
input_file = input("Enter the file name to read from: ")
output_file = input("Enter the file name to write to: ")
file_write = open(output_file, "w")
inputFile = open(input_file)
buffer = ""
for line in inputFile:
for i in line:
if i.isalpha():
buffer += bin(ord(i))[2:]
file_write.write(buffer)
file_write.close()
inputFile.close()
[2:]删除正在转换为输出文件的'0b'。