AnswerBun.com

stripping tabs, newlines, and spaces from string output, but leave one space so that words are not connected

Stack Overflow Asked by AndrewLittle1 on January 3, 2022

I have a list_3, with one element, a string:

[['nnn Headquarters or Regional OfficennnnntttttttttMain Headquarterstttttttnn', 'nnn FoundersnnnnntttttttttThomas Lon Vantttttttnn', 'nnn Founder DiversitynnnnntttttttttN/Atttttttnn', 'nnn Year Foundednnnnnttttttttt2016tttttttnn', 'nnn # of Employeesnnnnnttttttttt1-10tttttttnn', 'nnn Seeking Funding?nnnnntttttttttNo tttttttnn', 'nnn Funding PhasennnnntttttttttN/Atttttttnn'], ['nnn Headquarters or Regional OfficennnnntttttttttMain Headquarterstttttttnn', 'nnn FoundersnnnnntttttttttMacKenzie T Stout,tttttttnn', 'nnn Founder DiversitynnnnntttttttttN/Atttttttnn', 'nnn Year Foundednnnnnttttttttt2020tttttttnn', 'nnn # of Employeesnnnnnttttttttt1-10tttttttnn', 'nnn Seeking Funding?nnnnntttttttttYestttttttnn', 'nnn Funding PhasennnnntttttttttPre-Seedtttttttnn']]

I want to use regex to strip ntr, from the output and return the text in an easy to read format

This is what I have tried:

list_33 = []
for i in list_3:
     string = ''.join(list_3)
     list_33.append(re.sub('s+','', string))
print(list_33)

output:

['HeadquartersorRegionalOfficeMainHeadquarters', 'FoundersThomasLonVan', 'FounderDiversityN/A', 'YearFounded2016', '#ofEmployees1-10', 'SeekingFunding?No', 'FundingPhaseN/A']

This is almost what I need but I would like there to be one space between each word and colon after the first text block from list_3, ie:

['Headquarters or Regional Office: Main Headquarters', 'Founders: Thomas Lon Van', 'Founder Diversity: N/A', 'Year Founded: 2015', '# of Employees 1-10', 'Seeking Funding?: No', 'Funding Phase: N/A']

Any ideas of how I can incorporate both regex functions into one?

Thanks

ps. I know that I don’t need to use a for loop for a list with just one element, but in the future the list will have more elements, I am trying to generalize the code structure using just one input right now.

One Answer

You can navigate through each string in the list and the use re.sub to replace each occurrence of more than 2 white space by a :

>>> import re
>>> lst = ['nnn Headquarters or Regional OfficennnnntttttttttMain Headquarterstttttttnn', 'nnn FoundersnnnnntttttttttThomas Lon Vantttttttnn', 'nnn Founder DiversitynnnnntttttttttN/Atttttttnn', 'nnn Year Foundednnnnnttttttttt2016tttttttnn', 'nnn # of Employeesnnnnnttttttttt1-10tttttttnn', 'nnn Seeking Funding?nnnnntttttttttNo tttttttnn', 'nnn Funding PhasennnnntttttttttN/Atttttttnn']
>>> [re.sub(r'ss+', ': ', word).strip(': ') for word in lst]
['Headquarters or Regional Office: Main Headquarters', 'Founders: Thomas Lon Van', 'Founder Diversity: N/A', 'Year Founded: 2016', '# of Employees: 1-10', 'Seeking Funding?: No', 'Funding Phase: N/A']

Answered by Prem Anand on January 3, 2022

Add your own answers!

Related Questions

R mutate multiple columns with ifelse

1  Asked on December 7, 2021 by qnp1521

   

How can I show the code of my program in the output in python?

2  Asked on December 7, 2021 by ayaan-singh-saundh

   

Can’t unzip file with php

3  Asked on December 7, 2021 by ciro-garca

     

why isn’t my local variable overwriting the global?

4  Asked on December 7, 2021 by pillow-study

   

Variable not assigned even though I assigned it (Android Java)

1  Asked on December 7, 2021 by ictwinner

   

Function to find string with only one emoji

1  Asked on December 7, 2021 by user1199595

 

How to group buttons on Google Closure editor?

1  Asked on December 7, 2021 by frenchcooc

 

Mark non db fields @Transient vs extent JPA Entity?

1  Asked on December 7, 2021 by bluelurker

       

Ask a Question

Get help from others!

© 2023 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP, SolveDir