Python Forum
How to create new line '/n' at each delimiter in a string?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to create new line '/n' at each delimiter in a string?
#1
I want python to automatically import a new line, or /n code at each delimiter within a string. Lets say I want Python to make a new line at every 'period' it sees within the string...WITHOUT typing out the /n myself (I want Python to automatically import the new line)

Lets say I have a string

Quote:'I want to be a developer. I also want to learn AWS. I am going to practice joining strings.'

And at each period within the string, I want Python to create a new line, so it reads like this:

Quote:'I want to be a developer.
I also want to learn AWS.
I am going to practice joining strings.'

I know the split function can split the string, but I cant figure out how to import the /n code into each delimiter, or how to import new lines in python without coding it. How can I do this automatically rather than manually adding a /n to each line?
Reply
#2
This is slightly tricky, because at the end you only have a ".", but between the sentences you have ". ". So a simple substitution makes it look a bit wonky.

So I might first replace all ". " with ".", then replace those with ".\n".

s = 'I want to be a developer. I also want to learn AWS. I am going to practice joining strings.'
s = s.replace(". ", ".")
s = s.replace(".", ".\n")
print(s)
Output:
I want to be a developer. I also want to learn AWS. I am going to practice joining strings.
LastStopDEVS and sridhar like this post
Reply
#3
This could be done using split, construct and join.

- split string at delimiter
- construct stream of substrings with putting delimiter back
- join substrings using repalcement string

There are corner cases which must be tested/addressed but something along those lines is sufficient to start:

def formatter(text, delimiter='.', replacement='\n'):
    stream = (f'{token.strip()}{delimiter}' for token in text.split(delimiter) if
              token) 
    return replacement.join(stream)


s = 'I want to be a developer. I also want to learn AWS. I am going to practice joining strings.'

print(formatter(s)) ->
I want to be a developer.
I also want to learn AWS.
I am going to practice joining strings.
sridhar likes this post
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#4
Regex can be useful for this if need to build it with more rules.
Example let say it's ? instead of .,it should normally be new line there to.
I have done this before for a subtext parser i wrote,i had to add many rules that would have been very difficult to write with just string methods and loops.
import re

>>> s = 'I want to be a developer. I also want to learn AWS. I am going to practice joining strings.'
>>> result = re.sub(r'\.\s', r'\n', s)
>>>
>>> print(result)
I want to be a developer
I also want to learn AWS
I am going to practice joining strings.
So if get a new text with a new problem,can build on regex or just pass text to a new re.sub() if regex get to complex in one go.
>>> import re 
>>> 
>>> s = 'I want to be a developer. I also want to learn AWS. What If I Don’t Know If the Woman Is Married or Not? hello world a new line'
>>> result = re.sub(r'\.\s|\?\s', r'\n', s)
>>>
>>> print(result)
I want to be a developer
I also want to learn AWS
What If I Don’t Know If the Woman Is Married or Not
hello world a new line
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  python Read each xlsx file and write it into csv with pipe delimiter mg24 4 1,427 Nov-09-2023, 10:56 AM
Last Post: mg24
  Context-sensitive delimiter ZZTurn 9 1,444 May-16-2023, 07:31 AM
Last Post: Gribouillis
  Read csv file with inconsistent delimiter gracenz 2 1,192 Mar-27-2023, 08:59 PM
Last Post: deanhystad
  File "<string>", line 19, in <module> error is related to what? Frankduc 9 12,540 Mar-09-2023, 07:22 AM
Last Post: LocklearSusan
  Create Excel Line Chart Programmatically dee 3 1,179 Dec-30-2022, 08:44 PM
Last Post: dee
  Writing string to file results in one character per line RB76SFJPsJJDu3bMnwYM 4 1,364 Sep-27-2022, 01:38 PM
Last Post: buran
  Inserting line feeds and comments into a beautifulsoup string arbiel 1 1,182 Jul-20-2022, 09:05 AM
Last Post: arbiel
  Delimiter issue with a CSV file jehoshua 1 1,279 Apr-19-2022, 01:28 AM
Last Post: jehoshua
  Python code to read second line from CSV files and create a master CSV file sh1704 1 2,395 Feb-13-2022, 07:13 PM
Last Post: menator01
  append a string to a modified line Mr_Blue 10 3,838 Sep-16-2021, 07:24 PM
Last Post: Mr_Blue

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020