python - Append value only when not found -


if taxonomy in taxonomies not in translations. want print 152w00000x | not found of lines print not found. if remove else out of range error.

taxonomies = ['152w00000x', '156fx1800x', '200000000x', '261qd0000x', '3336c0003x', '333600000x', '261qd0000x'] translations = {'261qd0000x': 'clinic/center   dental', '3336c0003x': 'pharmacy        community/retail pharmacy',  '333600000x': 'pharmacy'} = 0 final = []  nums in taxonomies:     i, v in translations.items():             if nums == i:                     data = v                     final.append(data)             else:                     final.append('not found')  nums in taxonomies:     print nums, "|", final[a]     = + 1 

current output is:

152w00000x | not found 156fx1800x | not found 200000000x | not found 261qd0000x | not found 3336c0003x | not found 333600000x | not found 261qd0000x | not found 

the ideal output is:

152w00000x | not found 156fx1800x | not found 200000000x | not found 261qd0000x | clinic/center   dental 3336c0003x | pharmacy        community/retail pharmacy 333600000x | pharmacy 261qd0000x | clinic/center   dental 

taxonomies = ['152w00000x', '156fx1800x', '200000000x', '261qd0000x', '3336c0003x', '333600000x', '261qd0000x'] translations = {'261qd0000x': 'clinic/center   dental', '3336c0003x': 'pharmacy        community/retail pharmacy',  '333600000x': 'pharmacy'} = 0 final = []   nums in taxonomies:     final.append(translations.get(nums, 'not found'))  nums in taxonomies:     print nums, "|", final[a]     = + 1 

i using re split idvtaxo.txt @ 2 or more spaces. unless source delimited tabs work.

import re  open('idvtaxo.txt') f:     idvtaxo = {re.split(r'\s{2,}', x)[0]: re.split(r'\s{2,}', x)[2] x in f.read().splitlines()}  open('taxonomies.txt') f:     taxonomies = f.read().splitlines()  taxonomy in taxonomies:     data = taxonomy.split('|')     tranlated = idvtaxo.get(data[1], 'not found')     print '%s|%s' % (taxonomy, tranlated) 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -