I have a string:
feature.append(freq_and_feature(text, freq))
I want a list containing each word of the string, like [feature, append, freq, and, feature, text, freq], where each word is a string, of course.
These string are contained in a file called helper.txt, so I'm doing the following, as suggested by multiple SO posts, like the accepted answer for this one(Python: Split string with multiple delimiters):
import re
with open("helper.txt", "r") as helper:
for row in helper:
print re.split('\' .,()_', row)
However, I get the following, which is not what I want.
[' feature.append(freq_pain_feature(text, freq))\n']
re.split('\' .,()_', row)
This looks for the string ' .,()_
to split on. You probably meant
re.split('[\' .,()_]', row)
re.split
takes a regular expression as the first argument. To say "this OR that" in regular expressions, you can write a|b
and it will match either a
or b
. If you wrote ab
, it would only match a
followed by b
. Luckily, so we don't have to write '| |.|,|(|...
, there's a nice form where you can use []
s to state that everything inside should be treated as "match one of these".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With