1

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

Somdip Dey
  • 3,346
  • 6
  • 28
  • 60
  • 1
    Please add the code that actually generates the error. – chepner Jan 21 '20 at 15:57
  • We'll also need the file 'mycsv.csv' – Daniel Lima Jan 21 '20 at 16:00
  • @chepner i have added the code snippet. Thanks. – Somdip Dey Jan 21 '20 at 16:00
  • 1
    use string.maketrans() function before translate() – bihari_gamer Jan 21 '20 at 16:11
  • @ManishGupta actually i tried using string.maketrans(), however, even in the python doc it mentions: Don’t use strings derived from lowercase and uppercase as arguments; in some locales, these don’t have the same length. For case conversions, always use str.lower() and str.upper(). – Somdip Dey Jan 21 '20 at 16:21
  • Are you trying to replace `punctuation` with space or something? If that is the case then call `abstract.translate(''.maketrans(string.punctuation, ' ' *len(string.punctuation))).lower()`. – accdias Jan 21 '20 at 16:59

1 Answers1

1

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'
>>> 
accdias
  • 5,160
  • 3
  • 19
  • 31
  • Thank you for the answer. Actually str works for python 3 but not for python 2 and throws error. Since the original answer was based on python 2, that’s why I changed it to string rather than str. – Somdip Dey Jan 21 '20 at 17:48
  • Oh! I see. I missed that Python version completely. If you don't mind, go ahead and edit it again, please. – accdias Jan 21 '20 at 17:49