-1

I have the split the data that starts from ( and ends ) x contains data likes (33)Knoxville, TN,,,(1)Basking Ridge, NJ location = "".join(x.split("()"))[4:] in this split logic what condition should I gve [3:] ??

           if name:

        if x.startswith('(') and x.endswith(')'):

            location = "".join(x.split("()"))[3:]

            print(location)
        else:
            location = x
Jeyasri
  • 13
  • 3
  • you have `endswith()` two times but you don't use `startswith()` – furas Jul 18 '19 at 09:36
  • 1
    Please consider formatting the code correctly in your question, and don'f forget to post also the error traceback, as @furas said. – Alex Gidan Jul 18 '19 at 09:37
  • `split("()")` doesn't split on `"("` and `")"` but only on `"()"` – furas Jul 18 '19 at 09:38
  • 1
    `re.split(r'[()]', strin)` will do split on `(` or `)` – Avinash Raj Jul 18 '19 at 09:39
  • if you don't know what to use then use both with `print()` to see what you get in both situations. – furas Jul 18 '19 at 09:39
  • put code in question, not in comment. In question it will be readable and everyone will read it. Not everyone reads comments. – furas Jul 18 '19 at 09:41
  • 1
    edit your data to make them more readable and show expected result. – furas Jul 18 '19 at 09:42
  • use `print()` to see what you have after `x.split("()")` and after you use `join()` – furas Jul 18 '19 at 09:43
  • if you want to remove `(33)` and `(1)` then get `split(")")[1]` - it doesn't need `join()`. And use `print( split(")") )` to see what you get - to learn something. – furas Jul 18 '19 at 09:47
  • Please add an clear desired output. But also present you the example and input in amore concise way. You indent also don't make sense. – Herc01 Jul 18 '19 at 10:20

1 Answers1

0

Hope you are trying to split by (chars) or ,,

>>> s = '(1)Basking Ridge, NJ (33)Knoxville, TN'
>>> import re
>>> re.split(r'\s*\([^()]*\)\s*|\s*,\s*', s)
['', 'Basking Ridge', 'NJ', 'Knoxville', 'TN']
>>> t = re.split(r'\s*\([^()]*\)\s*|\s*,\s*', s)
>>> ','.join([i for i in t if i])
'Basking Ridge,NJ,Knoxville,TN'
>>> 
Avinash Raj
  • 172,303
  • 28
  • 230
  • 274