pos = 0
if len(r) > 1 and not r[0] in 'aeoAEO':
    pos = 1
if tone != 0:
    r = r[0:pos] + pinyinToneMarks[r[pos]][tone - 1] + r[pos + 1:]
return r + m.group(2)

def convertPinyin(s):
    return re.sub('([aeiou\xfcv\xdc]{1,3})(n?g?r?)([012345])',
        convertPinyinCallback, s, flags=re.IGNORECASE)
print(convertPinyin('Ni3 hao3 ma0?'))
Convert numbered pinyin to pinyin with tone marks