Search code examples
pythonstringunicodeconcatenationbackslash

Backslashes and unicode


I am making a romaji to hiragana translator and am getting an error when I try this concatenation. I made a list of keys and am using a for loop to make a dictionary by the sequential nature of unicode.

combos = {}
for hexy in range(12363, 12435):
    combos[sounds[12363 - hexy]] = ('\u%s' % str(chr(hex(hexy))))

I get the error SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 0-1: truncated \uXXXX escape

I have tried doubling the backslashes, separating the u and backslash, using a + instead of value insertion, and checking out when other people got similar errors.


Solution

  • If you want the character, just use chr(hexy).

    If you want an escape code and assuming Python 3.6+, use f'\\u{hexy:04x}'.

    for example (dropped sounds since it wasn't defined):

    combos = {}
    for i,hexy in enumerate(range(12363, 12435)):
        combos[i] = chr(hexy)
    print(combos)
    

    Result:

    {0: 'か', 1: 'が', 2: 'き', 3: 'ぎ', 4: 'く', 5: 'ぐ', 6: 'け', 7: 'げ', 8: 'こ', 9: 'ご', 10: 'さ', 11: 'ざ', 12: 'し', 13: 'じ', 14: 'す', 15: 'ず', 16: 'せ', 17: 'ぜ', 18: 'そ', 19: 'ぞ', 20: 'た', 21: 'だ', 22: 'ち', 23: 'ぢ', 24: 'っ', 25: 'つ', 26: 'づ', 27: 'て', 28: 'で', 29: 'と', 30: 'ど', 31: 'な', 32: 'に', 33: 'ぬ', 34: 'ね', 35: 'の', 36: 'は', 37: 'ば', 38: 'ぱ', 39: 'ひ', 40: 'び', 41: 'ぴ', 42: 'ふ', 43: 'ぶ', 44: 'ぷ', 45: 'へ', 46: 'べ', 47: 'ぺ', 48: 'ほ', 49: 'ぼ', 50: 'ぽ', 51: 'ま', 52: 'み', 53: 'む', 54: 'め', 55: 'も', 56: 'ゃ', 57: 'や', 58: 'ゅ', 59: 'ゆ', 60: 'ょ', 61: 'よ', 62: 'ら', 63: 'り', 64: 'る', 65: 'れ', 66: 'ろ', 67: 'ゎ', 68: 'わ', 69: 'ゐ', 70: 'ゑ', 71: 'を'}