Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maketrans not working for petl with python3.4

I am using petl package which i have installed using pip in virtulaenv with python 3.4. when i tried to test that whether the petl package is installed properly or not in the python shell I have done this to check

$ python 
Python 3.4.0 (default, Apr 11 2014, 13:05:11) 
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from petl import *
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/user/.env/lib/python3.4/site-packages/petl/__init__.py", line 10, in <module>
    from petl.util import header, fieldnames, data, records, rowcount, look, see, \
  File "/home/user/.env/lib/python3.4/site-packages/petl/util.py", line 14, in <module>
    from string import maketrans
ImportError: cannot import name 'maketrans'
>>>

I tried to check whether maketrans is present in string package i run this

>>> from string import maketrans
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: cannot import name 'maketrans'
>>> 

Found that the default python string package does not have this . I am not sure that why petl package have used this without mentioning it in its dependency and if its the default python package then why its giving import error.

Not sure what is happening can any one please help

like image 426
Mukesh'python-php Avatar asked Dec 18 '14 05:12

Mukesh'python-php


4 Answers

In Python2, maketrans is a function belongs to the string module. However in Python3, maketrans is a static method of the str type.

like image 167
Leonardo.Z Avatar answered Nov 07 '22 20:11

Leonardo.Z


Because I was looking for a clear example of how it works in python 3.4, I post what I've found:

#in py2 you need to "from string import maketrans" 
table = "".maketrans('cs', 'kz')
#py2 table = maketrans('cs', 'kz')
len(table)
#in py2 you will get **len(table) = 256 in py3.4 len(table) = 2**
sentence = "cause koala is causing trouble"
sentence.translate(table)
like image 9
piotrektt Avatar answered Nov 07 '22 19:11

piotrektt


use str to invoke maketrans

LETTERS = 'abcdefghijklmnopqrstuvwxyz'
NUMBERS = '22233344455566677778889999'
## translate a-z char to phone digits
TRANS = str.maketrans(LETTERS, NUMBERS)
like image 8
Shubham Avatar answered Nov 07 '22 20:11

Shubham


Update: petl >= 1.0 supports Python 3.4.


Clearly petl does not work on Python 3.x.

This specific error is because the Python 2.x string.maketrans function doesn't exist in 3.x.* But if you got past that, you'd find lots of other errors.

While the PyPI entry doesn't list supported versions (which it really should), a quick google turned up Issue #240, to add Python 3 support, which has been on the backlog since 26 Aug 2014. And a 2to3 pass over the source shows hundreds of issues.**

So, how do you fix this?

  1. Use something other than petl.
  2. Use Python 2.7 for your petl work.
  3. Help port it.

* Actually, in 3.0, it still existed, but only worked on bytes objects. In 3.1, maketrans and translate methods were added to bytes and bytearray, equivalent to the ones on str, and the string functions were deprecated, and then in 3.2 or 3.3 they were removed.

** Some of these issues are using things that were already deprecated in 2.6 or 2.7, which is odd given that petl originally worked only in 2.7, and was later ported to also work in 2.6.

like image 2
abarnert Avatar answered Nov 07 '22 21:11

abarnert