我从Numbers.txt文件中获取了输入,并希望在out.txt文件中写入输出, 任何人都可以指导出错的地方。
import num2word_EN as s
text = open("C:\\Users\\eanaaks\\Desktop\\Python Practice Program\\Numbers.txt","r")
outfile = open("C:\\Users\\eanaaks\\Desktop\\Python Practice Program\\out.txt", "w")
for line in text:
line = line.rstrip()
num = int(line)
print line
x = s.to_card(num)
print (x)
outfile.write("%s\n"%(line));
outfile.close()
text.close()
答案 0 :(得分:1)
以下是代码的改进版本:
import num2word_EN as s
input_file = 'C:\Users\eanaaks\Desktop\Python Practice Program\Numbers.txt'
output_file = 'C:\Users\eanaaks\Desktop\Python Practice Program\out.txt'
with open(input_file, 'r') as fin
with open(output_file, 'w') as fout:
for line in fin:
num = int(line)
print(line)
x = s.to_card(num)
print(x)
# What's the data type of x? int? string?
# This will write the original data and the processed data separated by tab.
fout.write('%s\t%s\n' % (line.rstrip(), x));