SyntaxError :( unicode error)'unicodeescape'编解码器无法解码位置292-293中的字节:截断\ UXXXXXXXX在导入期间转义

时间:2018-05-08 11:31:32

标签: python-3.x unicode

代码:

import os
import random
import time
import requests
from appetizer import Appetizer

结果:

    Traceback (most recent call last):
  File "C:/GITHUB/stress_testing/main.py", line 5, in <module>
    from appetizer import Appetizer
  File "C:\GITHUB\stress_testing\venv\lib\site-packages\appetizer\__init__.py", line 17, in <module>
    from .appetizer import Appetizer
  File "C:\GITHUB\stress_testing\venv\lib\site-packages\appetizer\appetizer.py", line 76
            """
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 292-293: truncated \UXXXXXXXX escape

开胃菜文件中的76行:

 def detect_adb(self):
        """ Detect the path to the adb tool from the Android SDK

        :return: A JSON object. For example: {'adb': '/home/myuser/Android/Sdk/platform-tools/adb'}

        Note that the path could be a unicode string.
        The default installation paths for different OSes are:
        Windows: C:\Users\<User Name>\AppData\Local\Android\sdk\platform-tools\
        Linux: /home/<User Name>/Android/Sdk/platform-tools/adb
        """
        return json.loads(self.appetizer.check_output(["adb", "detectadb"]))

我知道“\”(Windows路径中的双“\”),“/”等等。但是在我做完某事之前我有错误 - 在导入阶段。我应该改变什么,在哪里? 我试过虚拟环境和普通。

1 个答案:

答案 0 :(得分:1)

它实际上告诉你出了什么问题。在代码第76行的函数定义中,位置292-293处有一个\ uxxxxxxx转义字符,其长度无效。

以下内容可行:

stri = """ Detect the path to the adb tool from the Android SDK

    :return: A JSON object. For example: {'adb': '/home/myuser/Android/Sdk/platform-tools/adb'}

    Note that the path could be a unicode string.
    The default installation paths for different OSes are:
    Windows: C:\\Users\<User Name>\AppData\Local\Android\sdk\platform-tools\
    Linux: /home/<User Name>/Android/Sdk/platform-tools/adb
    """
stri[292] #'\\'
stri[293] #'U'

鉴于:

stri = """ Detect the path to the adb tool from the Android SDK

    :return: A JSON object. For example: {'adb': '/home/myuser/Android/Sdk/platform-tools/adb'}

    Note that the path could be a unicode string.
    The default installation paths for different OSes are:
    Windows: C:\Users\<User Name>\AppData\Local\Android\sdk\platform-tools\
    Linux: /home/<User Name>/Android/Sdk/platform-tools/adb
    """
#SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 292-293: truncated \UXXXXXXXX escape

这种行为的原因是(请在下次指出相关包){3}}是为Python 2.7编写的,其中'\U'会毫无障碍地通过。您必须检查源代码并用双反斜杠或手动正斜杠替换这些序列。