unicode error preventing creation of text file

前端 未结 2 546
难免孤独
难免孤独 2021-01-24 04:36

What is causing this error and how can I fix it?

(unicode error) \'unicodeescape\' codec can\'t decode bytes in position 2-3: truncated \\UXXXXXXXX escape

相关标签:
2条回答
  • 2021-01-24 05:08

    You should escape backslashes inside the string literal. Compare:

    >>> print("\U00000023")  # single character
    #
    >>> print(r"\U00000023") # raw-string literal with 
    \U00000023
    >>> print("\\U00000023") # 10 characters
    \U00000023
    
    >>> print("a\nb")  # three characters (literal newline)
    a
    b
    >>> print(r"a\nb") # four characters (note: `r""` prefix)
    a\nb
    
    0 讨论(0)
  • 2021-01-24 05:09

    \U is being treated as the start of a Unicode literal. Use a raw string (a preceding r) to prevent this translation:

    >>> 'C:\Users'
      File "<stdin>", line 1
    SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape
    >>> r'C:\Users'
    'C:\\Users'
    
    0 讨论(0)
提交回复
热议问题