1

I have a text file which contains some simple strings like

StatusInfoSet (2, 12), StatusInfoSet (2, 44) 

I would like delete any empty line in the file and rewrite it in below pattern to the same file as well as printing it.

StatusInfoSet (2, 12) StatusInfoSet (2, 44) 

I forgot to add that my data too has some string like (not all contain brackets)

 Hello, Hello, Hello, Hello, Hello 

Any guidance would be really helpful.

2 Answers 2

2

If strings in file is such consistent, you can simply use Python's native replace method and write to output something like

original_string.replace('), ', ')\n') 
Sign up to request clarification or add additional context in comments.

2 Comments

I edited my question as I too have data which has no brackets
This worked pretty fine with a small change as line = line.replace('), ', ')\n') line = line.replace('o, ', 'o\n')
0
,\s(?![^(]*\)) 

Try this.See demo.

http://regex101.com/r/hQ9xT1/15

x="StatusInfoSet (2, 12), StatusInfoSet (2, 44)" import re re.sub(r",\s(?![^(]*\))","\n",x) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.