-1

I am trying to write a function which can remove leading spaces and replace spaces in between the string with ',' and then split the string by ',' to individual elements.

split_entry = ['ENTRY', '      102725023         CDS       T01001']
res = []

def ws(list):
    for x in split_entry:
        entry_info = x.lstrip()                     #remove leading spaces at the start of string ('      102725023)
        entry_info = re.sub('\s+',', ',entry_info)  #replaces multiple spaces with ',' within a string
        if(re.search("^\d+",entry_info)):           #if element starts with digits '102725023'
            l = entry_info.split(", ", entry_info)                   #split by ','
            print (l[0])                            #get first element
            #return l[0]

ws(split_entry)

I would like to get the first element of the second list element (102725023 ). However I am getting following error while running above code.

TypeError: 'str' object cannot be interpreted as an integer

Any suggestions to resolve it. Thanks

rshar
  • 1,381
  • 10
  • 28
  • What is your expected output? `split` ***already*** strips leading spaces... – Tomerikoo Feb 16 '22 at 18:59
  • You do not need to, and should not try to, do anything with regex at all for this task. You should not try to replace the whitespace runs with commas. Instead, you should just use the `.split` method of strings, which **already handles whitespace runs in the exact way that you want, and also handles leading and trailing whitespace in the way that you want**. – Karl Knechtel Feb 16 '22 at 19:00
  • `' 102725023 CDS T01001'.split()` gives `['102725023', 'CDS', 'T01001']` and so `' 102725023 CDS T01001'.split()[0]` gives `'102725023'`. Not sure why need regex and so much complication... – Tomerikoo Feb 16 '22 at 19:01
  • 1
    As to the error, you are simply calling `split` wrong. What is the purpose of the extra `entry_info` argument? It should just be `entry_info.split(", ")` – Tomerikoo Feb 16 '22 at 19:03
  • As for the error message, you should [show complete error messages](https://meta.stackoverflow.com/questions/359146), copied and pasted starting from the line that says `Traceback (most recent call last):`, and formatted as code - after first attempting to [diagnose the problem yourself](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). But that problem is a simple typo: `entry_info.split(', ')` doesn't need another parameter (if you provided one, it should be the number of times to split). Maybe you were trying to use `re.split` instead? – Karl Knechtel Feb 16 '22 at 19:05

1 Answers1

0

You don't actually need to manually remove spaces your self - Python's split function can handle multiple spaces

After that, you can join the List to get a comma-separated string

a = 'fa     fdsa   f'
b = a.split()
print(b) # ['fa', 'fdsa', 'f']
print(",".join(b)) # 'fa,fdsa,f'

As for your code, there are a couple things:

  • Don't name your variable or parameter list because it is a reserved keyword in Python
  • In your ws function, you should use for x in parameterList, not explicitly stating split_entry

But honestly I am not entirely sure what exactly is wrong either.

Bao Huynh Lam
  • 974
  • 4
  • 12