控制台文本到csv文件类型错误:write()参数必须是str,

2024-09-26 22:42:05 发布

您现在位置:Python中文网/ 问答频道 /正文

任何建议都将不胜感激。我向控制台返回了一些文本,我想将字符串保存为csv文件。我试过几种不同的方法来拯救它,但都没有用。我尝试过的另一种方法是将输出转换为数组,但这也不起作用。如果您有关于将控制台文本返回csv文件的想法,请让我知道。谢谢


import os
from pdfminer3.layout import LAParams, LTTextBox
from pdfminer3.pdfpage import PDFPage
from pdfminer3.pdfinterp import PDFResourceManager
from pdfminer3.pdfinterp import PDFPageInterpreter
from pdfminer3.converter import TextConverter
import io
from PyPDF2 import PdfFileMerger, PdfFileReader

class Transform:
    #method for extracting data and merging it into one pdf

    def __init__(self):
        try:
            source_dir = os.getcwd()
            merger = PdfFileMerger()
            for item in os.listdir(source_dir):
                if item.endswith("pdf"):
                    merger.append(item)
        except Exception:
            print("unable to collect")
        finally:
            merger.write("test.pdf")
            merger.close()

#running that method extract
    def extract(self):
        resource_manager = PDFResourceManager()
        file = io.StringIO()
        converter = TextConverter(resource_manager, file, laparams=LAParams())
        page_interpreter = PDFPageInterpreter(resource_manager, converter)

        with open('test.pdf', 'rb') as fh:
            for page in PDFPage.get_pages(fh,
                                          caching=True,
                                          check_extractable=True):
                page_interpreter.process_page(page)

            text = file.getvalue()

        # close open handles
        converter.close()
        file.close()
        return text

    # def convertoarry(self, text):
    #     listToPrint = []
    #     for text in dict.keys():
    #         listToPrint.append(text)
    #         listToPrint.append(dict[text])
    #     stringToPrint = ",".join(listToPrint)
    #     return stringToPrint
    #
    #     stringToPrint = convertoarry(self, text)
    #     print(stringToprint)

    def modify(self, text):
        words = text.split()
        combine = [words.index()]
        with open("text.csv", "w") as f:
            f.write(combine)
            f.close()
            return "compeleted"


program = Transform()
print(program.modify())

Tags: csvtextfromimportselfforclosepdf
1条回答
网友
1楼 · 发布于 2024-09-26 22:42:05

我假设您得到的错误来自以下行:

            f.write(combine)

combine是一个list对象,而fwrite方法需要一个str对象。这就是你应该做的:

            f.write(str(combine))

但是,由于您正在创建csv,因此您可能不希望文件中出现“[”和“]”。此外,通过这样做,您还将在文件中包含您可能不想要的“'”。最安全的方法是在words上迭代,而不是创建combine

    def modify(self, text):
        words = text.split()
        with open("text.csv", "w") as f:
            f.write(words[0])
            for word in words[1:]:
                f.write(f", {word}")
            f.close()
            return "compeleted"

顺便说一下,如果您使用的是上下文管理器(即,如果您使用^{),则不需要明确地关闭文件。因此,您可以将此代码简化如下:

    def modify(self, text):
        words = text.split()
        with open("text.csv", "w") as f:
            f.write(words[0])
            for word in words[1:]:
                f.write(f", {word}")
        return "compeleted"

相关问题 更多 >

    热门问题