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: