tags:

views:

69

answers:

1
       new_str="@@2@@*##1"
       new_str1="@@3@@*##5##7"

How to split the above string in python

          for val in new_str.split("@@*"):
            logging.debug("=======")
            logging.debug(val[2:]) // will give 
            for st in val.split("@@*"):
                //how to get the values after ##  in new_str and new_str1
+2  A: 

I don't understand the question.

Are you trying to split a string by a delimiter? Then use split:

>>> a = "@@2@@*##1"
>>> b = "@@3@@*##5##7"
>>>
>>> a.split("@@*")
['@@2', '##1']
>>> b.split("@@*")
['@@3', '##5##7']

Are you trying to strip extraneous characters from a string? Then use strip:

>>> c = b.split("@@*")[1]
>>> c
'##5##7'
>>> c.strip("#")
'5##7'

Are you trying to remove all the hashes (#) from a string? Then use replace:

>>> c.replace("#","")
'57'

Are you trying to find all the characters after "##"? Then use rsplit with its optional argument to split only once:

>>> a.rsplit("##",1)
['@@2@@*', '1']
katrielalex
your last option to find the value is inferior because it "splits only once" and doesn't work on even the second example. Regular expressions are the way to go here ;)
aaronasterling
@Aaron: well, that depends on what the OP wants! Regex are nice, but they can be overkill, especially for simple problems like this.
katrielalex
I think it's pretty clear what OP wants and this is an ideal job for regex. specifically, "//how to get values after ## in new_str1 and new_str2" Now if there are _only ever going to be one or two copies of "##", then your solution works but why make that assumtion when regex are fast enough.
aaronasterling
`I think it's pretty clear what OP wants` Then you must be psychic
Falmarri