Somdip Dey
Somdip Dey

Reputation: 3386

ValueError: translation table must be 256 characters long

I have python flask project (program) where I am converting user's input string with capital letters and punctuations into a string without them. When I run the program I getting the following error:

ValueError: translation table must be 256 characters long

 Traceback (most recent call last) File:

"/home/sd18656/.local/lib/python2.7/site-packages/flask/app.py", line
2463, in __call__ return self.wsgi_app(environ, start_response) File
"/home/sd18656/.local/lib/python2.7/site-packages/flask/app.py", line
2449, in wsgi_app response = self.handle_exception(e) File
"/home/sd18656/.local/lib/python2.7/site-packages/flask/app.py", line
1866, in handle_exception reraise(exc_type, exc_value, tb) File
"/home/sd18656/.local/lib/python2.7/site-packages/flask/app.py", line
2446, in wsgi_app response = self.full_dispatch_request() File
"/home/sd18656/.local/lib/python2.7/site-packages/flask/app.py", line
1951, in full_dispatch_request rv = self.handle_user_exception(e) File
"/home/sd18656/.local/lib/python2.7/site-packages/flask/app.py", line
1820, in handle_user_exception reraise(exc_type, exc_value, tb) File
"/home/sd18656/.local/lib/python2.7/site-packages/flask/app.py", line
1949, in full_dispatch_request rv = self.dispatch_request() File
"/home/sd18656/.local/lib/python2.7/site-packages/flask/app.py", line
1935, in dispatch_request return
self.view_functions[rule.endpoint](**req.view_args) File
"/home/sd18656/FlaskProject/mainapp.py", line 61, in home score,
total_processed_data = get_score(abstract) File
"/home/sd18656/FlaskProject/mainapp.py", line 32, in get_score
abstract = abstract.translate(string.punctuation).lower()

In the program, abstract is a string type. I came across this solution: What does "table" in the string.translate function mean? for the error, however, the string.maketrans does not seem to go well with lower() orupper(). How can I fix this issue?

The code snippet which is causing this issue as follows:

r = reader(open('mycsv.csv','r'))
abstract_list = []
score_list = []
institute_list = []
row_count = 0
for row in list(r)[1:]:
    institute,score,abstract = row[0], row[1], row[2]
    if len(abstract.split()) > 0:
      institute_list.append(institute)
      score = float(score)
      score_list.append(score)
      abstract = abstract.translate(string.punctuation).lower()
      abstract_list.append(abstract)
      row_count = row_count + 1

Content of mycsv.csv is something as follows:

enter image description here

Upvotes: 1

Views: 525

Answers (1)

accdias
accdias

Reputation: 5372

You need to pass a translation table to str.translate (string.translate in Python 2). The translation table is nothing more than a dict where the key is the search character and value is the replacement, where key and value are the ord() of the respective characters.

If you want to replace all punctuation characters with space, for example, do something like this:

from string import punctuation
transtable = string.maketrans(punctuation, ' ' * len(punctuation))

abstract = abstract.translate(transtable).lower()

Here is a proof of concept:

>>> from string import punctuation
>>> transtable = string.maketrans(punctuation, ' ' * len(punctuation))
>>> type(transtable)
<class 'dict'>
>>> 'This!is#a.string,with;punctuations:'.translate(transtable).lower()
'this is a string with punctuations '
>>> 

Here is another usage example of maketrans and translate (just for fun):

>>> elite = string.maketrans('aeiou', '4310v')
>>> 'Hackers Rulez'.translate(elite)
'H4ck3rs Rvl3z'
>>> 

Upvotes: 1

Related Questions