python - 写入后文本文件不显示
问题描述
我编写了一个函数,它获取一个 pdf 文件,提取文本并将其保存到用户选择的文本文件中。该功能运行良好,但似乎没有创建文本文件。该函数没有返回错误,因此我认为该文件没有按应有的方式出现。
def pdf_to_text(pdf_file, output_txt):
file_content = [] # list to save text
# import pdf file, extract text and pass into a .txt
pdf_reader = PDF2.PdfFileReader(pdf_file)
# get page and content
for i in range(pdf_reader.getNumPages()):
page = pdf_reader.getPage(i)
p_content = page.extractText().replace("\n", "")
file_content.append(p_content)
return file_content
# save to text file
with open(output_txt, "w") as output:
output.write("".join([i for i in file_content]))
注意最后两行。我认为这些之前的一切都很完美;我什至将内容作为证据,并且效果很好。但是没有创建文本文件。
更新
此外,在不删除换行符的情况下,我得到如下输出:
(
g
) (U)
Release
deploying soldiers to 1
st
Army for deployment to
OSR AO.
3. (U)
End State.
虽然它应该是这样的:
(g) (U) Release deploying soldiers to 1st Army for deployment to OSR AO.
3. (U) End State.
解决方案
推荐阅读
- python - cv2.estimateRigidTransform with fullAffine=False
- postgresql - 聚集索引可以在移动到不同的表空间后存活吗?
- python - 使用 python 读取 xml 元素
- php - Joomla & PHP 7:致命错误:参数 1 传递给 JErrorPage::render()
- android - Firebase 多级子查询 Android
- python - 终端中的python3子进程pip“ImportError:无法导入名称main”
- python - Python3中多处理进程的实例变量未在终止时写入
- python - Matplotlib 刺/轴切割
- powerbi - Maxtrix - 如何将列移动到行
- swift - 如何在使用 swift 播放音乐时获取 iPhone 的当前音量并更改它?