2

I'm wondering if there's a way, while iterating through a string with a for loop, to remove a portion of the string and do something else with it.

Basically, I want to start with one string "first portion|middle|,end" and end with two strings. The "buffer" variable should contain "first portion,end" and the second string should contain "|middle|"

word = "first portion|middle|,end"
buffer = ''

for i in word:
    if i == '|':
        # Loop in here until another pipeline is found while saving each character
    else:
        buffer += i 

I already have a program that parses the original string and organizes it so ideally I just want to have an if statement in the for loop that will remove anything between pipelines and save it. If this isn't possible I will rewrite the program to allow me too do this.

4 Answers 4

1

Are you looking for str.split() function ?

You can pass in the PIPE character to it and it would split the string at the places where it finds | and return you the list of split strings. Example -

>>> word = "first portion|middle|,end"
>>> word.split('|')
['first portion', 'middle', ',end']

Then if you are sure that there would always only be three elements, take the first and third element, and concatenate them for the first string result you want, and use the second element for the second result. Example -

>>> word = "first portion|middle|,end"
>>> l = word.split('|')
>>> result1 = l[0] + l[2]
>>> result2 = l[1]
>>> result1
'first portion,end'
>>> result2
'middle'
Sign up to request clarification or add additional context in comments.

Comments

0

This is not very optimized but it would do the trick.

import re
middle = re.findall('\|[a-zA-Z]+\|', s)[0]
other = re.split('\|[a-zA-Z]+\|', s)

Comments

0

If you are sure that your string in the form of "str1|str2|str3" you can use split

>>> l = "first portion|middle|,end".split('|')
>>> l[0]+l[-1]
'first portion,end'
>>> l[1]
'middle'

Comments

0

You could use a regular expression if the string structure is always the same.

The regexp (.*)(\|.*\|)(.*) will capture any text contained between two pipes and text before and after it ( (.*) captures any text string with 0 or more characters).

>>> import re
>>> p = re.compile(ur'(.*)(\|.*\|)(.*)')
>>> test_str = u"first portion|middle|,end"
>>> strings = p.match(test_str).groups()

Here strings will have the parts you wanted:

>>> strings
(u'first portion', u'|middle|', u',end')
>>> buffer = strings[0] + strings[2]
>>> second = strings[1]

>>> buffer
u'first portion,end'
>>> second
u'|middle|'

You can test regular expressions online on sites like regex101, though I'd recommend using them for text of which you know the structure.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.