Python Forum
The number of occurrences of statistical characters
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
The number of occurrences of statistical characters
#4
You can also use the re.escape method, to escape the strings before building a regex with it.

Before:
>>> import re
>>> [re.compile(r"[{0}]".format(chr(i))) for i in range(33, 126)]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 1, in <listcomp>
  File "C:\Users\_\AppData\Local\Programs\Python\Python35-32\lib\re.py", line 224, in compile
    return _compile(pattern, flags)
  File "C:\Users\_\AppData\Local\Programs\Python\Python35-32\lib\re.py", line 293, in _compile
    p = sre_compile.compile(pattern, flags)
  File "C:\Users\_\AppData\Local\Programs\Python\Python35-32\lib\sre_compile.py", line 536, in compile
    p = sre_parse.parse(p, flags)
  File "C:\Users\_\AppData\Local\Programs\Python\Python35-32\lib\sre_parse.py", line 829, in parse
    p = _parse_sub(source, pattern, 0)
  File "C:\Users\_\AppData\Local\Programs\Python\Python35-32\lib\sre_parse.py", line 437, in _parse_sub
    itemsappend(_parse(source, state))
  File "C:\Users\_\AppData\Local\Programs\Python\Python35-32\lib\sre_parse.py", line 545, in _parse
    source.tell() - here)
sre_constants.error: unterminated character set at position 0
After:
>>> [re.compile(r"[{0}]".format(re.escape(chr(i)))) for i in range(33, 126)]
[re.compile('[\\!]'), re.compile('[\\"]'), re.compile('[\\#]'), re.compile('[\\$]'), re.compile('[\\%]'), re.compile('[\\&]'), re.compile("[\\']"), re.compile('[\\(]'), re.compile('[\\)]'), re.compile('[\\*]'), re.compile('[\\+]'), re.compile('[\\,]'), 
#snipped
Reply


Messages In This Thread
RE: The number of occurrences of statistical characters - by nilamo - Jan-18-2018, 10:10 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Find if chain of characters or number Frankduc 4 1,752 Feb-11-2022, 01:55 PM
Last Post: Frankduc
  Count number of occurrences of list items in list of tuples t4keheart 1 2,343 Nov-03-2020, 05:37 AM
Last Post: deanhystad
  Count & Sort occurrences of text in a file oradba4u 7 3,006 Sep-06-2020, 03:23 PM
Last Post: oradba4u
  Translation of R Code to Python for Statistical Learning Course SterlingAesir 2 2,092 Aug-27-2020, 08:46 AM
Last Post: ndc85430
  Remove escape characters / Unicode characters from string DreamingInsanity 5 13,421 May-15-2020, 01:37 PM
Last Post: snippsat
  Counting number of occurrences of a single digit in a list python_newbie09 12 5,380 Aug-12-2019, 01:31 PM
Last Post: perfringo
  Occurrences using FOR and IF cycle P86 2 2,477 Jul-29-2019, 04:37 PM
Last Post: ThomasL
  Split Column Text by Number of Characters cgoldstein 3 2,949 Mar-11-2019, 01:45 PM
Last Post: perfringo
  Printing Easter date occurrences samsonite 8 4,915 Mar-06-2019, 11:49 AM
Last Post: samsonite
  Counting number of characters in a string Drone4four 1 3,411 Aug-16-2018, 02:33 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020