【问题标题】:How to merge seperate string objects into a list in Python如何将单独的字符串对象合并到 Python 中的列表中
【发布时间】:2019-09-06 19:03:08
【问题描述】:

作为最终产品,我有一个名为“members”和“pcps”的对象,它们本身实际上是一堆单独的字符串对象。我需要将它们矢量化为一个列表,以便我可以将它们添加到数据框并最终作为 Excel 表

当我从 PDF 中抓取文本数据时,问题出现在某个地方,它没有作为列表中的列表的数据结构。想知道在我尝试创建“成员”系列的那一行,我是否可以以某种方式将这些单独的对象合并到一个列表中。


def PDFsearch(origFileName): 

    # creating a pdf File object of original pdf 
    pdfFileObj = open(origFileName, 'rb')  
    # creating a pdf Reader object 
    pdfReader = PyPDF2.PdfFileReader(pdfFileObj) 

    numPages = pdfReader.numPages
    print(numPages)
    for p in range(pdfReader.numPages): 

        # creating page object 
        pageObj = pdfReader.getPage(p)
        #extract txt from pageObj into unicode string object
        pages = pageObj.extractText()
        # loop through string object by page
        pges = []


        for page in pages.split("\n"):
            # split the pages into words
            pges.append(page)

            lns = []            
            for lines in page.split(" "):
                for line in lines.split(","):   #seperate the ,"This" from the last name
                    lns.append(line)

            names = list()
            if lns[0] == "Dear":   # If first word in a line is "Dear"
                names.append(lns[1:4]) # Get the 2nd and 3rd items (First and Last names)              
                for name in names:
                    members = " ".join(name) # These are the names we want

                PCPs = lns[78:85]        
                pcps = " ".join(PCPs)

                providers =  pd.Series(pcps)
                members = pd.Series(members)

'''This is what I get when I print the series 'members':

0    LAILIA TAYLOR 
dtype: object
0    LATASIA WILLIS 
dtype: object
0    LAURYN ALLEN 
dtype: object
0    LAYLA ALVARADO 
dtype: object
0    LAYLA BORELAND 
dtype: object
0    LEANIAH MULLIGAN 
dtype: object

All separate objects!  Same with 'providers'.  and when I create a dataframe and export to excel I only get one row'''

【问题讨论】:

    标签: python list data-structures merging-data


    【解决方案1】:

    快速浏览一下,但我相信您的问题是您每次都在覆盖您的系列。试试这样的:

    # add at the beginning of your function 
    temp = pd.DataFrame()
    data = pd.DataFrame()
    
    # this would replace where you assign to providers and members
    temp['providers'] = pd.Series(pcps)
    temp['members'] = pd.Series(members)
    data = pd.concat([data, temp]).reset_index(drop=True)
    

    这样您每次都会覆盖 temp,但您的数据 DataFrame 将包含所有成员和提供者。我希望这会有所帮助,祝你好运!

    【讨论】:

    • 这似乎有效,但它一次又一次地重复,每次重复每一行。我应该将这些系列声明和连接放在哪个缩进选项卡上,以使其每个成员只有一次?
    • 如果不处理您的代码和您的数据是什么样的,则不是肯定的,但您可以尝试将其放在第二个 for 循环之外,以便它在第二个 for 循环结束后直接发生,但如果您的代码是重复相同的信息,你不需要它给你很多想做的事情。同样,如果没有看到数据输入和输出,就很难说。
    猜你喜欢
    • 2021-11-07
    • 2019-03-27
    • 2020-10-11
    • 2021-04-07
    • 2021-10-07
    • 2022-07-06
    • 2020-04-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多