Joining output of iterative list in a while loop

Viewed 75

I have a text file that I'm extracting text from using its punctuation and indentation patterns. The output should be a list of lists combining two lists; company_name and description

[[company,description],[company,description]]

To do that I'm running a while loop nested within a for loop to extract the description for each company. Here's my code

for line in file:
if not re.search(r"            ", line, re.MULTILINE):
        name = line.split(',', 1)[0]
        companies.append(name)
        print(companies)
        companies = []
    while re.search(r"            ", line, re.MULTILINE):
        desc.append(line)
        print(desc)
        desc = []
        break 

Sample from text file:

XYZ Group, a nearly nine-year-old, Copenhagen-based company that has built a dual-purpose platform, providing both accountancy software and a marketplace for small and medium businesses to find accountants, has landed $73 million in growth funding from a single investor, Lugard Road Capital. TechCrunch has more here.

Black Lake, a nearly five-year-old, China-based software platform for factory workers to log their daily tasks and managers to oversee the plant floor, recently raised $77 million in funding, including from Singapore’s sovereign wealth fund Temasek, which led the round, as well as China Renaissance and Lightspeed Venture Partners. The outfit has now raised more than $100 million altogether, including from from GGV...

Actual text file with indentation pattern That's the output:

['XYZ Group']

['            company that has built a dual-purpose platform, providing both']
['            accountancy software and a marketplace for small and medium']
['            businesses to find accountants, has landed 73 million in growth funding from a single investor,']
['            Lugard Road Capital TechCrunch has more']
['            here']

['Black Lake']

['            platform for factory workers to log their daily tasks and managers']
['            to oversee the plant floor, recently raised 77 million in funding,']
['            including from Singapore’s sovereign wealth fund Temasek,']
['            which led the round, as well as China']
['            Renaissance and Lightspeed Venture']
['            Partners The outfit has now raised more than 100']
['            million altogether, including from from GGV']
['            Capital, Bertelsmann Asia Investments,']
['            GSR Ventures, ZhenFund']
['            and others TechCrunch has more']
['            here']

The goal is to join the output of desc list under company name into 1 list

Update

I put desc = [] outside of the while loop and I'm getting this:

['XYZ Group']
['            company that has built a dual-purpose platform, providing both']
['            company that has built a dual-purpose platform, providing both', '            accountancy software and a marketplace for small and medium']
['            company that has built a dual-purpose platform, providing both', '            accountancy software and a marketplace for small and medium', '            businesses to find accountants, has landed 73 million in growth funding from a single investor,']
['            company that has built a dual-purpose platform, providing both', '            accountancy software and a marketplace for small and medium', '            businesses to find accountants, has landed 73 million in growth funding from a single investor,', '            Lugard Road Capital TechCrunch has more']
['            company that has built a dual-purpose platform, providing both', '            accountancy software and a marketplace for small and medium', '            businesses to find accountants, has landed 73 million in growth funding from a single investor,', '            Lugard Road Capital TechCrunch has more', '            here']

I only need the last iteration though

1 Answers

Assuming the text is always following a <company_name>, <description> pattern, a very simple approach based on .split(). Simply split on the first , by limiting the number of splits with maxsplit=1 to get the name and full_description which can be prettified afterwards:

text = "XYZ Group, a nearly nine-year-old, Copenhagen-based company that has built a dual-purpose platform, providing both accountancy software and a marketplace for small and medium businesses to find accountants, has landed $73 million in growth funding from a single investor, Lugard Road Capital. TechCrunch has more here."

name, full_description = text.split(',', 1)
description = [s.strip() for s in full_description.split(',')]

output = [name, description]
print(output)

Output:

['XYZ Group', ['a nearly nine-year-old', 'Copenhagen-based company that has built a dual-purpose platform', 'providing both accountancy software and a marketplace for small and medium businesses to find accountants', 'has landed $73 million in growth funding from a single investor', 'Lugard Road Capital. TechCrunch has more here.']]

Alternatively, you could also use .split(" ") to split on the multiple occurring spaces and ignore any commas.

Related