A
A
Alexey Poloz2017-04-02 22:04:31
Python
Alexey Poloz, 2017-04-02 22:04:31

Python How to get string from NoneType?

I attached a module to the program, I get p.POS from it
When outputting, it writes a string, for example 'NOUN', or gives None
I need to add my string to this value, but an error appears: TypeError: Can't convert 'NoneType' object to str implicitly
This class is .TypedGrammeme'>
QUESTION - How can I convert any returned value to a string?
list, str, ... doesn't help

Answer the question

In order to leave comments, you need to log in

3 answer(s)
G
gill-sama, 2017-04-03
@kosyachniy

def convert(obj):
    try:
        return str(obj)
    except TypeError:
        return ''

V
vais, 2017-04-02
@vais

none_type=None

def convert_none_type(item):
  if not item or item =='NOUN':
    return ''
  return item

print(convert_none_type(none_type)+'abc')

V
Vlad Grigoriev, 2017-04-04
@Vaindante

item_test = None
print((item_test if item_test else '') + ' add_str')
>>> add_str

Or
item_test = None
print('%s_%s' % (item_test, ' add_str'))
>>> None_ add_str

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question