Python split string on whitespace and punctuation
I'm trying to split a string up into words and punctuation, adding the punctuation to the list produced by the split. Show For instance:
What I really want the list to look like is:
So, I want the string split at whitespace with the punctuation split from the words. I've tried to parse the string first and then run the split:
This produces the result I want, but is painfully slow on large files. Is there a way to do this more efficiently?
Fionnuala 89.7k7 gold badges108 silver badges145 bronze badges asked Dec 14, 2008 at 23:30
1
This is more or less the way to do it:
The trick is, not to think about where to split the string, but what to include in the tokens. Caveats:
answered Dec 15, 2008 at 1:53 4 Here is a Unicode-aware version:
The first alternative catches sequences of word characters (as defined by unicode, so "résumé" won't turn into Note that, unlike the top answer, this treats the single quote as separate punctuation (e.g. "I'm" -> answered Jan 19, 2012 at 17:58
LaCLaC 12.5k5 gold badges37 silver badges38 bronze badges 1 If you are going to work in English (or some other common languages), you can use NLTK (there are many other tools to do this such as FreeLing).
sh37211 1,2611 gold badge15 silver badges34 bronze badges answered Nov 8, 2018 at 16:16
1 Here's my entry. I have my doubts as to how well this will hold up in the sense of efficiency, or if it catches all cases (note the "!!!" grouped together; this may or may not be a good thing).
One obvious optimization would be to compile the regex before hand (using re.compile) if you're going to be doing this on a line-by-line basis. answered Dec 15, 2008 at 1:30 1 Here's a minor update to your implementation. If your trying to doing anything more detailed I suggest looking into the NLTK that le dorfier suggested. This might only be a little faster since ''.join() is used in place of +=, which is known to be faster.
answered Dec 15, 2008 at 1:05
monkutmonkut 40.3k23 gold badges118 silver badges148 bronze badges 3 This worked for me
answered Apr 21, 2020 at 8:41
MalgoMalgo 1,5331 gold badge14 silver badges28 bronze badges I think you can find all the help you can imagine in the NLTK, especially since you are using python. There's a good comprehensive discussion of this issue in the tutorial. answered Dec 15, 2008 at 0:34
dkretzdkretz 37.1k13 gold badges79 silver badges136 bronze badges I came up with a way to tokenize all words and
Here Note the following though -- this will group punctuation that consists of more than one symbol:
Of course, you can find and split such groups with:
answered Apr 15, 2014 at 19:11
FrauHahnhenFrauHahnhen 1332 silver badges11 bronze badges Try this:
Aurasphere 3,68112 gold badges42 silver badges69 bronze badges answered Apr 18, 2017 at 9:03
Have you tried using a regex? http://docs.python.org/library/re.html#re-syntax By the way. Why do you need the "," at the second one? You will know that after each text is written i.e. [0] "," [1] "," So if you want to add the "," you can just do it after each iteration when you use the array.. answered Dec 14, 2008 at 23:34
Filip EkbergFilip Ekberg 35.8k19 gold badges123 silver badges181 bronze badges In case you are not allowed to import anything,use this!
Kosuke Sakai 2,1912 gold badges4 silver badges11 bronze badges answered Nov 27, 2019 at 9:14
How do you split a string by punctuation and spaces in Python?findall() method to split a string into words and punctuation, e.g. result = re. findall(r"[\w'\"]+|[,.!?] ", my_str) . The findall() method will split the string on whitespace characters and punctuation and will return a list of the matches.
How do you split a string with spaces and commas?To split a string by space or comma, pass the following regular expression to the split() method - /[, ]+/ . The method will split the string on each occurrence of a space or comma and return an array containing the substrings.
How do you split a string in whitespace in Python?Python String split() Method
The split() method splits a string into a list. You can specify the separator, default separator is any whitespace. Note: When maxsplit is specified, the list will contain the specified number of elements plus one.
Can I split a string by two delimiters Python?To split a string with multiple delimiters in Python, use the re. split() method. The re. split() function splits the string by each occurrence of the pattern.
|