bcicen/wikitables

Python2 compatibility

Closed this issue · 7 comments

When I execute the example, I get an error:

python
Python 2.7.11+ (default, Apr 17 2016, 14:00:29) 
[GCC 5.3.1 20160413] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from wikitables import import_tables
>>> tables = import_tables('List of cities in Italy')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/moose/.local/lib/python2.7/site-packages/wikitables/__init__.py", line 24, in import_tables
    return list(_table_gen())
  File "/home/moose/.local/lib/python2.7/site-packages/wikitables/__init__.py", line 22, in _table_gen
    yield WikiTable(name, table)
  File "/home/moose/.local/lib/python2.7/site-packages/wikitables/models.py", line 98, in __init__
    self._read(raw_table)
  File "/home/moose/.local/lib/python2.7/site-packages/wikitables/models.py", line 115, in _read
    row = Row(self.head, tr)
  File "/home/moose/.local/lib/python2.7/site-packages/wikitables/models.py", line 72, in __init__
    super(Row, self).__init__(self._read(head, self.raw))
  File "/home/moose/.local/lib/python2.7/site-packages/wikitables/models.py", line 84, in _read
    return zip(head, [ Field(c) for c in cols ])
  File "/home/moose/.local/lib/python2.7/site-packages/wikitables/models.py", line 20, in __init__
    self.value = self._read(self.raw)
  File "/home/moose/.local/lib/python2.7/site-packages/wikitables/models.py", line 36, in _read
    return ' '.join(list(_read_parts()))
  File "/home/moose/.local/lib/python2.7/site-packages/wikitables/models.py", line 34, in _read_parts
    val = self._read_part(n).strip(' \n')
  File "/home/moose/.local/lib/python2.7/site-packages/wikitables/models.py", line 49, in _read_part
    return str(node.title)
  File "/home/moose/.local/lib/python2.7/site-packages/mwparserfromhell/string_mixin.py", line 61, in __str__
    return bytes(self.__unicode__())
UnicodeEncodeError: 'ascii' codec can't encode character u'\xec' in position 4: ordinal not in range(128)
>>> 

Wikitables is written in Pyhton3.
I too got the same error in Python2 possibly because in python3 strings are sequence of unicode characters whereas in python2, strings are sequence of ascci characters.

I got the same error, but it went away when I ran with python3.

Wikitables is indeed a Python3 library; however, I hope to have Python2 support available soon.

Please edit the readme to prevent more disappointment while we wait for a port. BTW thanks for the great idea, wiki parsing has never been easier.

@Hunter-Github good point; I've added a note to the readme for the time being.

Fixed in #6, but keeping this open to track other Py2 compat issues

Updates for Python2 compatibility have been included in the latest release.