首页 > 编程问答 >编解码器无法解码位置 2-3 中的字节:截断的 \UXXXXXXXX 转义 (Python QREADER)

编解码器无法解码位置 2-3 中的字节:截断的 \UXXXXXXXX 转义 (Python QREADER)

时间:2024-07-21 10:47:04浏览次数:17  
标签:python qr-code unicode-escapes pyzbar

我正在尝试 python qreader 模块,但每次我尝试运行它时,我都会收到 SyntaxError: (unicode error) 'unicodeescape' codec can't Decode bytes inposition 2-3: truncated \UXXXXXXXX escape error.

from qreader import QReader
from cv2 import QRCodeDetector, imread
from pyzbar.pyzbar import decode

# Initialize the three tested readers (QRReader, OpenCV and pyzbar)
qreader_reader, cv2_reader, pyzbar_reader = QReader(), QRCodeDetector(), decode

for img_path in ('C:\Users\Blabla\Desktop\code\python_shit\out.png'):
    # Read the image
    img = imread(img_path)

    # Try to decode the QR code with the three readers
    qreader_out = qreader_reader.detect_and_decode(image=img)
    cv2_out = cv2_reader.detectAndDecode(img=img)[0]
    pyzbar_out = pyzbar_reader(image=img)
    # Read the content of the pyzbar output (double decoding will save you from a lot of wrongly decoded characters)
    pyzbar_out = tuple(out.data.data.decode('utf-8').encode('shift-jis').decode('utf-8') for out in pyzbar_out)

    # Print the results
    print(f"Image: {img_path} -> QReader: {qreader_out}. OpenCV: {cv2_out}. pyzbar: {pyzbar_out}.")

how can我解决这个问题了吗?

我有点迷失了,我不知道该怎么办


The error message "SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape" usually occurs when you have a raw string literal containing a backslash followed by a 'U' character in your file path.

The problem lies in how Windows represents file paths and how Python interprets backslashes. Here's the breakdown:

  1. Windows File Paths: Windows uses backslashes ( \ ) as path separators.
  2. Python String Literals: Python interprets a backslash ( \ ) as an escape character. So, when it encounters \U in your file path, it tries to interpret it as a Unicode escape sequence. However, \U requires eight hexadecimal digits to be a valid Unicode escape sequence, leading to the "truncated \UXXXXXXXX escape" error.

Here are a few ways to fix this issue:

1. Use Raw Strings: Prefix your file path string with an r to make it a raw string. Raw strings treat backslashes literally.

python img_path = r'C:\Users\Blabla\Desktop\code\python_shit\out.png' img = imread(img_path)

2. Use Forward Slashes: Python accepts forward slashes ( / ) as path separators on all operating systems, including Windows.

python img_path = 'C:/Users/Blabla/Desktop/code/python_shit/out.png' img = imread(img_path)

3. Escape the Backslashes: Use double backslashes ( \\ ) to represent a single backslash in your file path.

python img_path = 'C:\\Users\\Blabla\\Desktop\\code\\python_shit\\out.png' img = imread(img_path)

Choose one of these methods to modify your img_path variable, and the error should be resolved. Remember to use the chosen method consistently throughout your code when handling file paths.

标签:python,qr-code,unicode-escapes,pyzbar
From: 78774287

相关文章

  • Python 工程师对 3D 高斯溅射的介绍(第 1 部分)
    从Python工程师的角度理解和编写GaussianSplatting欢迎来到雲闪世界。2023年初,来自法国蔚蓝海岸大学和马克斯普朗克信息研究所的作者发表了一篇题为“用于实时场渲染的3D高斯溅射”的论文。¹该论文展示了实时神经渲染的重大进步,超越了NeRF等先前方法的实用性。²......
  • 使用Python读取PDF文件,部分内容显示为一串乱码。我应该如何恢复它?
    使用Python读取PDF文件,部分内容显示为一串乱码。我该如何恢复它?importfitzdoc=fitz.open("2303.11366v4.pdf")#downloadfromhttps://arxiv.org/pdf/2303.11366print(doc[2].get_text().split('Figure1')[0])我得到了这样的文字:<RXDUHLQWKHPLGGOHRIDURRP>@7DVN......
  • Python 迭代列表
    分配sum_extra给定列表test_grades收到的额外学分总额。满分是100分,所以超过100分都是额外分。对于给定程序,sum_extra是8,因为1+0+7+0是8。给定程序的示例输出:额外总和:8请原谅我,我是编码新手,而且真的很糟糕!这是我的代码(不起作用)请......
  • 计算机毕业设计Python+Spark新能源汽车推荐系统 汽车大数据 汽车数据分析 汽车可视化
    表2黄河交通学院本科毕业设计(论文)开题报告学生姓名刘丹杰专业班级20本大数据一班学号2080910T01521设计(论文)题目基于Hadoop的新能源汽车销售数据分析系统的设计与实现选题的目的和意义:选题目的:新能源汽车销售数据分析系统的设计与实现旨在利用Hadoop等大数......
  • 为什么我的 python 程序一直说没有名为“PIL”的模块?
    我正在pythonIDLEshell中工作,由于某种原因pyautogui.locateOnScreen将无法工作。这是我的代码:frompyautoguiimport*importpyscreezeimportpyautoguiimporttimeimportkeyboardimportrandomimportwin32api,win32conwhile1:ifpyautogui.locateOnS......
  • 我在 Python 时间格式化函数中遇到代码问题
    我一直在研究一个Python函数,将给定的秒数转换为可读的时间格式(HH:MM:SS)。该函数对于大多数测试用例都能正常工作,但对于一些特定的输入会失败。这是我编写的函数:defmake_readable(seconds):ifseconds<60:s1=secondsh1,m1=(0,0)return......
  • 《流畅的Python》第二版 第11章
     fromarrayimportarrayimportmathclassVector2d:__match_args__=('x','y')typecode='d'def__init__(self,x,y):self.__x=float(x)self.__y=float(y)@propertydefx(self)......
  • 《流畅的Python》第二版 第12章
       fromarrayimportarrayimportmathimportfunctoolsimportitertoolsimportoperatorimportreprlibclassVector:typecode='d'def__init__(self,components):self._components=array(self.typecode,components)......
  • 如何在Python中给jupyter单元标准输入?
    我正在尝试在接受用户输入的jupyter笔记本上运行一个程序,但我不知道如何让它读取标准输入。例如,如果我使用shift-enter:a=input()print(a)运行代码,则单元格指示它正在运行,但不接受我的输入。我如何让它接受输入?你遇到的问题是Jupyternotebook中的代码单元默认......
  • Python:如何从 csvreader 列表中删除括号和单引号?
    Pythonn00b在这里。尝试使用csvreader从文件导入数组并打印一个值,但它添加了括号和单引号。这是我的代码:importrandomimportcsvwithopen('crimes.csv','r')ascsvfile:crimes=list(csv.reader(csvfile))hello=["Hello","Greetings","Hi&q......