Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Mr Sri
9,200 PointsHow to separate large blocks of text with python
Hi all,
I just recently joined Treehouse and have started on the Python course! (First time learning anything to do with CompSci) I have begun to learn quite a lot but Im actually kind of confused on where to start with this problem I have at work.
At work I am usually provided with a large block of text and it comes in the following format:
add roaming||adding roaming||purchase roaming||roaming offer||roaming checklist||how much does roaming cost||
These blocks of text run usually quite long and can cover as much as 4-5 word docs. I am trying to figure out a way to write a script that will go through the block of text and separate the various phrases by placing them onto new lines by the double piping. So for the text above I want the script to print out:
add roaming||
adding roaming||
purchase roaming||
roaming offer||
roaming checklist||
how much does roaming cost||
Any guidance on this would be greatly appreciated!
Thanks, Sagivan
1 Answer
Andreas cormack
Python Web Development Techdegree Graduate 33,011 PointsHi Sagivan
I came up with something like this
def splitPipeString(mystring):
my_list= mystring.split('||')
my_new_string=""
for word in my_list:
my_new_string+=word+"||"+"\n"
return my_new_string
print(splitPipeString("add roaming||adding roaming||purchase roaming||roaming offer||roaming checklist||how much does roaming cost||"))
Line 2 -> i split the string by the || creating a list line 3 -> will hold the new appended output string line 4 and 5 -> loop around the list and append each word in the format you want. line 6 - > return the string
this script creates an extra line with a || that is because your block of text ends with a ||, you can fix this my removing the trailing || from the end of your block of text before you pass it to the function.
hope this helps
Mr Sri
9,200 PointsMr Sri
9,200 PointsThanks Andreas!
Mr Sri
9,200 PointsMr Sri
9,200 PointsThanks Andreas!