Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 生成和读取具有特殊字符的二维码_Python_Character Encoding_Qr Code - Fatal编程技术网

Python 生成和读取具有特殊字符的二维码

Python 生成和读取具有特殊字符的二维码,python,character-encoding,qr-code,Python,Character Encoding,Qr Code,我正在编写Python程序,该程序执行以下操作: 创建二维码>保存到png文件>打开文件>读取二维码信息 然而,当代码上的数据有特殊字符时,我得到了一些混乱的输出数据。这是我的密码: import pyqrcode from PIL import Image from pyzbar.pyzbar import decode data = 'Thomsôn Gonçalves Ámaral,325.432.123-21' file_iso = 'QR_ISO.png' file_utf =

我正在编写Python程序,该程序执行以下操作:

创建二维码>保存到png文件>打开文件>读取二维码信息

然而,当代码上的数据有特殊字符时,我得到了一些混乱的输出数据。这是我的密码:

import pyqrcode
from PIL import Image
from pyzbar.pyzbar import decode


data = 'Thomsôn Gonçalves Ámaral,325.432.123-21'

file_iso = 'QR_ISO.png'
file_utf = 'QR_Utf.png'

#creating QR codes
qr_iso = pyqrcode.create(data) #creates qr code using iso-8859-1 encoding
qr_utf = pyqrcode.create(data, encoding = 'utf-8') #creates qr code using utf-8 encoding
#saving png files
qr_iso.png(file_iso, scale = 8)
qr_utf.png(file_utf, scale = 8)

#Reading  and Identifying QR codes

img_iso = Image.open(file_iso)
img_utf = Image.open(file_utf)

dec_iso = decode(img_iso)
dec_utf = decode(img_utf)

# Reading Results:

print(dec_iso[0].data)
print(dec_iso[0].data.decode('utf-8'))
print(dec_iso[0].data.decode('iso-8859-1'),'\n')

print(dec_utf[0].data)
print(dec_utf[0].data.decode('utf-8'))
print(dec_utf[0].data.decode('iso-8859-1'))
以下是输出:

b'Thoms\xee\x8c\x9e Gon\xe8\xbb\x8blves \xef\xbe\x81maral,325.432.123-21'
Thoms Gon軋lves チmaral,325.432.123-21
Thoms Gon軋lves ï¾maral,325.432.123-21 

b'Thoms\xef\xbe\x83\xef\xbd\xb4n Gon\xef\xbe\x83\xef\xbd\xa7alves \xef\xbe\x83\xef\xbc\xbbaral,325.432.123-21'
Thomsテエn Gonテァalves テ[aral,325.432.123-21
Thomsテエn Gonテァalves テ[aral,325.432.123-21
Thomsôn Gonçalves Ámaral,325.432.123-21
对于简单的数据,它工作得很好,但当数据具有像“Á,ç”之类的字符时,就会发生这种情况。 我该怎么做才能修复它

其他信息:

  • 我正在使用Python3.8和PyIDE
  • 当我使用Android应用程序扫描生成的代码时,它可以很好地读取这两个代码
  • 我读过这个话题:但没什么帮助

尝试使用shift jis对UTF-8解码结果进行编码,并使用UTF-8再次解码结果

dec_utf[0].data.decode('utf-8').encode('shift-jis').decode('utf-8')
这至少适用于QR码也使用UTF-8的示例


另请参见

好的!获得了一些更新:

简短版本: @user14091216的答案似乎解决了这个问题。该行:

dec_utf[0].data.decode('utf-8').encode('shift-jis').decode('utf-8')
进行双重解码,解决了问题。我做了很多测试,没有任何错误。新代码如下所示

我尝试并发现的-长版本: 在与一些同事交谈后,他们认为我的数据是双重编码的。我仍然不知道为什么会发生这种情况,但就我所读到的,pyzbar lib在读取带有特殊字符的数据时似乎有问题

我尝试的第一件事是使用BOM(字节顺序标记): 根据我的原始代码,使用以下行:

data = '\xEF\xBB\xBF' + 'Thomsôn Gonçalves Ámaral,325.432.123-21'
qr_iso = pyqrcode.create(data) #creates qr code using iso-8859-1 encoding as standard    
qr_iso.png(file_iso, scale = 8)
img_iso = Image.open(file_iso)
dec_iso = decode(img_iso)
print(dec_iso[0].data.decode('utf-8'))
这就是结果:

b'Thoms\xee\x8c\x9e Gon\xe8\xbb\x8blves \xef\xbe\x81maral,325.432.123-21'
Thoms Gon軋lves チmaral,325.432.123-21
Thoms Gon軋lves ï¾maral,325.432.123-21 

b'Thoms\xef\xbe\x83\xef\xbd\xb4n Gon\xef\xbe\x83\xef\xbd\xa7alves \xef\xbe\x83\xef\xbc\xbbaral,325.432.123-21'
Thomsテエn Gonテァalves テ[aral,325.432.123-21
Thomsテエn Gonテァalves テ[aral,325.432.123-21
Thomsôn Gonçalves Ámaral,325.432.123-21
请注意,尽管我使用“iso-8859-1”编码创建了二维码,但它只有在解码为“utf-8”时才起作用。 我还需要处理这些数据,删除BOM表。这很容易,但这是一个额外的步骤。值得一提的是,对于更简单的数据(没有特殊字符),输出中没有“ï»字符”

上面的解决方案是可行的,但至少对我来说,它似乎并不完全正确。我用的是因为我没有更好的

我甚至尝试对数据进行双重解码: 基于“python双重解码”搜索,我尝试过这样的代码(以及一些变体):

但这些都不起作用

修复方法: 根据建议,我尝试了以下几行:

dec_utf[0].data.decode('utf-8').encode('shift-jis').decode('utf-8')
它工作得很好。我用1800多个数据字符串对它进行了测试,没有出现任何错误。 二维码生成似乎很好。这行代码仅在读取QR图像时处理pyzbar库的输出数据(不需要是pyqrcode库专门创建的QR代码)

我还没能用同样的技术解码“iso-8859-1”编码生成的二维码。我可能与pyzbar有关,或者我根本没有发现哪一个是解码过程的正确模式

下面是一个基于utf-8编码的创建和读取二维码的简单代码:

import pyqrcode
from PIL import Image
from pyzbar.pyzbar import decode


data = 'Thomsôn Gonçalves Ámaral,325.432.123-21'

file_utf = 'QR_Utf.png'

#creating QR codes
qr_utf = pyqrcode.create(data, encoding = 'utf-8') #creates qr code using utf-8 encoding

#saving png file
qr_utf.png(file_utf, scale = 8)

#Reading  and Identifying QR code

img_utf = Image.open(file_utf)
dec_utf = decode(img_utf)

# Decoding Results:

print(dec_utf[0].data.decode('utf-8').encode('shift-jis').decode('utf-8'))
有关更多信息,请参见:

嘿!谢谢你的建议和见解。这似乎有效。我将用更多的数据运行这段代码,并很快发布一些更新。谢谢你的帮助!