无法读取 python 中的输出文件
Unable to read the output file in python
无法读取输出文件。代码看起来很完美,但我既没有得到想要的结果,也没有得到错误。请帮我解决这个问题
注意:我在打印
时得到的结果是空白space
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)
根据您在评论中指定的要求,我更新了我的代码。它在每一行中搜索 "Technical Skills",一旦找到,就可以写入 (copy
)。一旦命中这一行,它将写入每一行,当找到带有 "Work Experience" 的行时,它将停止处理。
with open("in.txt") as infile, open ("out.txt", 'w') as outfile:
copy = False
for line in infile:
if line.strip() == "Technical Skills":
copy = True
if line.strip() == 'Work Experience':
break
if copy:
outfile.write(line)
fh = open("out.txt", 'r')
contents = fh.read()
print(contents)
鉴于此输入:
Introduction
I'm me, I'm not sane, I do reckless things most of the time, and I have a
very hard time keeping focused on tasks.
Technical Skills
- I have this skill
- and this skill
- I suck at these skills
- but I'm very good at this, that and the other too
Work Experience
2001 - 2010 Senior Network Engineer
2010 - 2015 Senior Toilet Cleaner
它将产生这个输出:
Technical Skills
- I have this skill
- and this skill
- I suck at these skills
- but I'm very good at this, that and the other too
如果您不想在输出中出现 "Technical Skills" 行,请在 copy = True
行之后直接添加带有 continue
的行。
首先,循环应该是:
for line in infile:
接下来,正如Christopher Shroba等人提到的,你不应该在for循环中处理文件的opening/closing。 "with" 关键字是一个处理程序,因此它将处理打开和关闭文件。就像你已经写的那样。
infile 也是如此。 "With"正在处理,无需手动打开或关闭。
无法读取输出文件。代码看起来很完美,但我既没有得到想要的结果,也没有得到错误。请帮我解决这个问题
注意:我在打印
时得到的结果是空白spacewith 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)
根据您在评论中指定的要求,我更新了我的代码。它在每一行中搜索 "Technical Skills",一旦找到,就可以写入 (copy
)。一旦命中这一行,它将写入每一行,当找到带有 "Work Experience" 的行时,它将停止处理。
with open("in.txt") as infile, open ("out.txt", 'w') as outfile:
copy = False
for line in infile:
if line.strip() == "Technical Skills":
copy = True
if line.strip() == 'Work Experience':
break
if copy:
outfile.write(line)
fh = open("out.txt", 'r')
contents = fh.read()
print(contents)
鉴于此输入:
Introduction
I'm me, I'm not sane, I do reckless things most of the time, and I have a
very hard time keeping focused on tasks.
Technical Skills
- I have this skill
- and this skill
- I suck at these skills
- but I'm very good at this, that and the other too
Work Experience
2001 - 2010 Senior Network Engineer
2010 - 2015 Senior Toilet Cleaner
它将产生这个输出:
Technical Skills
- I have this skill
- and this skill
- I suck at these skills
- but I'm very good at this, that and the other too
如果您不想在输出中出现 "Technical Skills" 行,请在 copy = True
行之后直接添加带有 continue
的行。
首先,循环应该是:
for line in infile:
接下来,正如Christopher Shroba等人提到的,你不应该在for循环中处理文件的opening/closing。 "with" 关键字是一个处理程序,因此它将处理打开和关闭文件。就像你已经写的那样。
infile 也是如此。 "With"正在处理,无需手动打开或关闭。