Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python split with multiple delimiters not working

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']
like image 668
Jobs Avatar asked Dec 24 '22 07:12

Jobs


1 Answers

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".

like image 68
Justin Avatar answered Jan 05 '23 19:01

Justin