mnooner256 / pyqrcode

Python 3 module to generate QR Codes
BSD 3-Clause "New" or "Revised" License
410 stars 76 forks source link

Unicode data does not work #17

Closed heuer closed 9 years ago

heuer commented 9 years ago
>>> import pyqrcode
>>> c = pyqrcode.create('Märchenbuch')
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/[...]/pyqrcode/__init__.py", line 78, in create
    return QRCode(content, error, version, mode)
  File "/[...]/pyqrcode/__init__.py", line 143, in __init__
    self.version = self._pick_best_fit()
  File "/[...]/pyqrcode/__init__.py", line 211, in _pick_best_fit
    capacity >= len(self.data.encode('ascii'))) or \
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe4' in position 1: ordinal not in range(128)
heuer commented 9 years ago

Fixed in 1.1

thanosgn commented 6 years ago

Unicode data work in python2 but python3 seems to crash.

See examples below.

$ python2
Python 2.7.15rc1 (default, Apr 15 2018, 21:51:34) 
[GCC 7.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import pyqrcode
>>> pyqrcode.create('John’s Pizza')
QRCode(content=u'John\u7ab6\u51b1 Pizza', error='H', version=2, mode='binary')
$ python3 
Python 3.6.5 (default, Apr  1 2018, 05:46:30) 
[GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pyqrcode
>>> pyqrcode.create('John’s Pizza')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.6/dist-packages/pyqrcode/__init__.py", line 111, in create
    return QRCode(content, error, version, mode, encoding)
  File "/usr/local/lib/python3.6/dist-packages/pyqrcode/__init__.py", line 161, in __init__
    self.data = content.encode(self.encoding)
UnicodeEncodeError: 'latin-1' codec can't encode character '\u2019' in position 4: ordinal not in range(256)