regex - removing words with numbers and letters from command line input in python 3: error -
i'm having trouble getting regex remove words contain digits , letters. keep getting "typeerror: expected string or buffer" can provide appreciated.
$ testing abc sorted_word = re.sub("\s+\d\s+", "", word_sort).strip() file "/usr/lib64/python2.6/re.py", line 151, in sub return _compile(pattern, 0).sub(repl, string, count) typeerror: expected string or buffer #! /usr/bin/env python import os import sys import re in_list = sys.argv def word_sort(in_list): word_sort = " 1a " word_sort = sorted(in_list[1:], key=len) in word_sort: punctuation = '.',',',';','!',' / ','"','?' #strips punctuation words if in punctuation: #removes punctuation word_sort = word_sort.replace(i," ") word_sort= sorted(word_sort, key=lambda l: (l.lower(), l)) sorted_word = " 1a " sorted_word = re.sub("\s+\d\s+", "", word_sort).strip() return sorted_word print (word_sort(in_list))
this:
word_sort= sorted(word_sort, key=lambda l: (l.lower(), l))
iterates through word_sort
string doesn't create str
object, list
of sorted characters, re
module chokes on it.
you have join
characters again recompose string:
word_sort= "".join(sorted(word_sort, key=lambda l: (l.lower(), l)))
small tester:
>>> sorted("dcba") ['a', 'b', 'c', 'd'] >>> "".join(sorted("dcba")) 'abcd'
btw: should avoid call function , local variables same name word_sort
. it's difficult read. , fortunately don't call function recursively :)
Comments
Post a Comment