Convert numeric strings to superscript

后端 未结 1 830
轻奢々
轻奢々 2020-12-06 19:31

I need convert numeric strings to superscript, is there a better (built-in) way of doing it?

def to_sup(s):
    sups={u\'1\': u\'\\xb9\', 
          u\'0\':          


        
相关标签:
1条回答
  • 2020-12-06 20:20

    Your way is slightly incorrect. Better would be:

    def to_sup(s):
        sups = {u'0': u'\u2070',
                u'1': u'\xb9',
                u'2': u'\xb2',
                u'3': u'\xb3',
                u'4': u'\u2074',
                u'5': u'\u2075',
                u'6': u'\u2076',
                u'7': u'\u2077',
                u'8': u'\u2078',
                u'9': u'\u2079'}
    
        return ''.join(sups.get(char, char) for char in s)  # lose the list comprehension
    

    s.isdigit() will only check the first letter, which probably doesn't make sense.

    If for some reason you want a one-liner:

    u''.join(dict(zip(u"0123456789", u"⁰¹²³⁴⁵⁶⁷⁸⁹")).get(c, c) for c in s)
    
    0 讨论(0)
提交回复
热议问题