I have the following problem I am trying to solve. I have a list of iso-639 languages that I retreived using langdetect
with the following code
def try_detect(cell):
try:
detected_lang = detect(cell)
except:
detected_lang = None
return detected_lang
Spotify['language'] = Spotify['artists'].apply(try_detect)
Spotify['language'] = Spotify['language'].str.upper()
Spotify['language'].unique()
which returned
array(['DE', 'PL', 'ES', 'EN', 'NL', 'TR', 'FR', 'IT', 'SK', 'RO', 'SW',
'FI', 'AF', 'EL', 'ID', 'LT', 'CA', 'TL', 'PT', 'HR', 'RU', 'NO',
'DA', 'SL', 'CY', 'SQ', 'KO', 'SO', 'CS', 'ET', 'ZH-CN', 'SV',
'HU', 'LV', 'VI', 'JA', None, 'AR', 'TH', 'BG'], dtype=object)
Although that would be sufficient, I'd love to have the full language name in another column. But, I do not seem to be able to get this right. I know that
pycountry.languages.get(alpha_2='FR').name
returns French
. I this tried:
Languages = Spotify['language'].unique()
LANG = []
for lang in Languages:
Lang = pycountry.languages.get(alpha_2=lang).name
LANG.append(Lang)
but I keep getting the error:
AttributeError: 'NoneType' object has no attribute 'name'
I'm at a loss there. Any help to put me on the right track would be greatl appriciated.