TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'

0 votes

Hi guys i have below code . trying to separate number,alphabet,alphanumeric from the string

import re

test_str = " Delivery Centre Strategy & Operations  - 17762-DCSO"

 

def manipulate(test_str):

    label = test_str.lstrip()

    number =''

    albhabet=''

    res = re.split(r'[-_&\s]\s*', label)

    print(res)

    while("" in res):

        res.remove("")

    print(res)

    for i in res:

        i=i.lstrip()

        print("i ->", type(i),i )

        if i.isalpha():

            albhabet +=i

            albhabet +=' '

        elif i.isdigit():

            print(type(i))

            number = number + i

            print(type(number))

        elif i.isalnum:

            al=''

            nu=''

            for k in range(0,len(i)):

                if i[k].isdigit():

                    nu +=i[k]

                elif i[k].isalpha():

                    al +=i[k]

            number +=nu

            albhabet +=al

            albhabet +=" "

      if not number:

            number = None

    return number,albhabet

result = manipulate(test_str)

app_name = result[1]

ARI = result[0]

print (ARI,app_name)

i am getting the below error for this code :

number = number + i
TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'

Could you suggest why i getting this error as i am sure that 'i' is of string type

Oct 30 in Python by sumanth
• 190 points
170 views

I tried the code, worked fine for me. Here's the output:

['Delivery', 'Centre', 'Strategy', '', 'Operations', '', '17762', 'DCSO']
['Delivery', 'Centre', 'Strategy', 'Operations', '17762', 'DCSO']
i -> <class 'str'> Delivery
i -> <class 'str'> Centre
i -> <class 'str'> Strategy
i -> <class 'str'> Operations
i -> <class 'str'> 17762
<class 'str'>
<class 'str'>
i -> <class 'str'> DCSO
17762 Delivery Centre Strategy Operations DCSO 

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In Python

0 votes
1 answer

Python error: unsupported operand type(s) for -: 'str' and 'str'

Hi. This error is caused because you ...READ MORE

answered May 12 in Python by Anjali
2,781 views
0 votes
1 answer

Python error "TypeError: Cannot compare types 'ndarray(dtype=int64)' and 'str'"

Hey @Ashish, change the emotion_map to the ...READ MORE

answered May 30 in Python by Mir
501 views
0 votes
1 answer
+1 vote
4 answers

How can I concatenate str and int objects?

If you want to concatenate int or ...READ MORE

answered Oct 18, 2018 in Python by subhm
55 views
0 votes
1 answer

What are the differences between type() and isinstance()?

To summarize the contents of other (already ...READ MORE

answered Jun 4, 2018 in Python by ariaholic
• 7,340 points
45 views
0 votes
1 answer

What are the differences between type() and is instance()?

Normally, in Python, you want your code ...READ MORE

answered Aug 6, 2018 in Python by Priyaj
• 56,940 points
31 views
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 347 views
+4 votes
6 answers