0

I have a variable like this:

bug = "^bug:url1.com;url2.com;url3.com;url4.com^"

And I want the end result to be like this:

bug = ["url1.com","url2.com","url3.com","url4.com"]

I tried:

#!/usr/bin/python

bug = "^bug:url1.com;url2.com;url3.com;url4.com^"
bug = bug.split(";")
print bug

But it outputs:

['^bug:url1.com', 'url2.com', 'url3.com', 'url4.com^']

Please note that the variable bug consists of a bunch of URLs not just ordinary words, maybe with regex ? I don't know sorry I'm still new to programming, please help me fix this.

4
  • 5
    bug[5:-1].split(';') ? Commented May 12, 2017 at 13:29
  • 1
    If you want to cut off ^bug: from the beginning and ^ from the end, write bug = bug[5:-1] and then split with bug.split(';') Commented May 12, 2017 at 13:29
  • 1
    That's not an array of characters; it's an array of strings. Commented May 12, 2017 at 14:01
  • Does this answer your question? How to split a string into an array of characters in Python? Commented Jul 15, 2020 at 15:14

5 Answers 5

2

I think the existing answers are too complicated for this simple task so I'm posting my comment as an answer:

>>> bug = "^bug:url1.com;url2.com;url3.com;url4.com^"
>>> bug[5:-1].split(';')
['url1.com', 'url2.com', 'url3.com', 'url4.com']

You slice the unwanted characters from the beginning and end of your string and afterwards you split the string by your delimiter ;. Of course, if there's anything dynamic about the format of your string, e.g. it could start with '^someunwantedtext:', then use a regular expression.

Sign up to request clarification or add additional context in comments.

3 Comments

Problem is "the variable bug consists of a bunch of URLs not just ordinary words" so bug might not be always of the same size. Your solution will only work if you know the length of URLs which if I understand well is not the case here.
@Nuageux no, this solution is independent from the length of the urls. You discard the first 5 and the last character from the string. What's in between does not matter.
My mistake you are right. I misunderstood some part of the question. I thought that "bug" could be something else that just "bug". I'm not a native english speaker, sorry.
0

First, split to remove part before ':' and to remove part after '^'. Then split for each ';'

bug = "^bug:url1.com;url2.com;url3.com;url4.com^"
bug = bug.split(":")[1:][0].split("^")[:-1][0]
bug = bug.split(";")
print bug
# ['url1.com', 'url2.com', 'url3.com', 'url4.com']

Comments

0

You could use lstrip() and rstrip(). This way you could even have ^ and bug inside the url and it won't be stripped.

bug = "^bug:url1.com;url2.com;url3.com;url4.com^"
buglist = bug.lstrip("^bug").lstrip(":").rstrip("^").split(";")

Output: ['url1.com', 'url2.com', 'url3.com', 'url4.com']

Comments

0

You can actually use regex for this purpose! replace the bug and special character in your data and split the urls with ;

import re
bug = "^hi.com;hi.com:url1.com;url2.com;url3.com;url4.com^"
print re.sub(r'((\w+.com;?)*:)|\^','',bug).split(';')

Output:

['url1.com', 'url2.com', 'url3.com', 'url4.com']

Comments

0

A combination of replace and split does the job:

>>> s = "^bug:url1.com;url2.com;url3.com;url4.com^"
>>> s.replace('^','').replace('bug:','').split(';')
['url1.com', 'url2.com', 'url3.com', 'url4.com']

Step-by-step explanation

>>> s.replace('^','')
'bug:url1.com;url2.com;url3.com;url4.com'
>>> s.replace('^','').replace('bug:','')
'url1.com;url2.com;url3.com;url4.com'
>>> s.replace('^','').replace('bug:','').split(';')
['url1.com', 'url2.com', 'url3.com', 'url4.com']
>>>

A better solution

As timgeb mentioned my method fails if the urls contain the string "bug:". timgeb's solution (https://stackoverflow.com/a/43939538/2194843) seems to be fine:

>>> s[5:-1].split(';')
['url1.com', 'url2.com', 'url3.com', 'url4.com']

1 Comment

Will fail if any url contains the substring 'bug:'.

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.