首页 > 解决方案 > 写入后文本文件不显示

问题描述

我编写了一个函数,它获取一个 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

解决方案


推荐阅读