Squidward
Squidward

Reputation: 131

How do I split a string into an array of characters in python?

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.

Upvotes: 0

Views: 437

Answers (5)

funk
funk

Reputation: 2287

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']

Upvotes: 0

Keerthana Prabhakaran
Keerthana Prabhakaran

Reputation: 3787

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']

Upvotes: 0

timgeb
timgeb

Reputation: 78650

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.

Upvotes: 2

L.S.
L.S.

Reputation: 506

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']

Upvotes: 0

Nuageux
Nuageux

Reputation: 1686

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']

Upvotes: 0

Related Questions