【发布时间】:2015-09-09 14:32:15
【问题描述】:
无法读取输出文件。代码看起来很完美,但我既没有得到期望的结果,也没有得到错误。请帮我解决这个问题
注意:我在打印时得到的结果是空格
with open("D:/txt_res/RahulChoudhary.txt") as infile, open ("x", 'w') as outfile:
copy = False
for line in infile:
if line.strip() == "Technical Skills":
copy = True
elif line.strip() == "Work Experience":
copy = False
elif copy:
outfile.write(line)
infile = open("x", 'r')
contents = infile.read()
print(contents)
【问题讨论】:
-
为什么要在 for 循环中关闭 outfile?你需要'a'模式吗?
-
@AnandSKumar 既没有错误也没有被执行
-
你能用 infile 更新问题吗?
-
不要在循环中关闭outfile,否则写完一行就写不下去了。
-
@LittleQ 据我所知,我们必须关闭输出文件,以便写入函数正常运行并将输出写入'x'。
标签: python information-retrieval data-retrieval