Extracting only characters from a string in Python
string.split() doesn't take regular expressions. You want something like:
re.split("[^a-zA-Z]*", "your string")
and to get a string:
" ".join(re.split("[^a-zA-Z]*", "your string"))
What about doing this?
>>> import ast
>>> " ".join([k[0] for k in ast.literal_eval("{('players',): 24, ('year',): 28, ('money',): 19, ('ipod',): 36, ('case',): 23, ('mini',): 46}").keys()])
'case mini year money ipod players'
I think that you want all words, not characters.
result = re.findall(r"(?i)\b[a-z]+\b", subject)
Explanation:
"
\b # Assert position at a word boundary
[a-z] # Match a single character in the range between “a” and “z”
+ # Between one and unlimited times, as many times as possible, giving back as needed (greedy)
\b # Assert position at a word boundary
"
You could do it with re, but the string split method doesnt take a regex, it takes a string.
Heres one way to do it with re:
import re
word1 = " ".join(re.findall("[a-zA-Z]+", st))