Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

When I Run My Code I Get A Huge Error In Python

If I Run This Code:

import re
mapping = {'gde1': 'a', '4&TW': 'b', 'E#ZB': 'c', 'B0F&': 'd', 'uvRD': 'e', 'M7vr': 'f', 'X$*d': 'g', 'XQ%R': 'h', 'jma+': 'i', 'P9We': 'j', 'xw4O': 'k', 'zT@3': 'l', '4B30': 'm', 'Jc&$': 'n', 'Szb7': 'o', '+yvf': 'p', 'tGgQ': 'q', '$g0Y': 'r', '9y6h': 's', '%Mo&': 't', '*fyQ': 'u', 'jaDZ': 'v', '%FaT': 'w', 'FBn5': 'x', 'sX2s': 'y', 'bFwN': 'z', 'qV0c': 'A', 'Zwg0': 'B', 'v!6!': 'C', 'wKKw': 'D', 'v3xd': 'E', 'oy@u': 'F', 'bu+S': 'G', '4Sh2': 'H', '6drQ': 'I', 'oQ#w': 'J', '555k': 'K', 'G$d3': 'L', 'AuJ3': 'M', 'XGm+': 'N', 'aqs3': 'O', '9Tvb': 'P', 'H%42': 'Q', 'g8E+': 'R', 'kwgO': 'S', '86+y': 'T', 'bv5e': 'U', 'Hg4b': 'V', '=dsc': 'W', 'B6jk': 'X', '@Av9': 'Y', 'kwPS': 'Z', 'Y1q$': '1', 'bNny': '2', 'mu!R': '3', 'HHRK': '4', 'm$!V': '5', '71D$': '6', 'm6qA': '7', 'o@U4': '8', '#ze1': '9', 'aAM6': '0', 'u7$q': '~', 'jwzQ': ':', 'yw#S': "'", '65BO': '+', 'xAhB': '[', 'X=G+': '\\', 'EXa7': '@', 'WpX6': '^', 'C#c2': '{', 'C8!y': '%', 'rCEB': '(', 'Ebos': '-', 'VKb!': '"', 'WPOu': '*', '9Ht$': '|', 'ZHo=': ',', 'ZkKY': '&', 'artq': '<', 'nS9m': '`', 'k!vX': '}', 'j1f6': '.', 'VpV%': '_', '5u*$': '=', '8rjs': ']', 'JJc!': '!', 'SW%$': '>', '#g@G': ';', '#xF&': '?', '$E60': '#', 'U7Su': '$', 'g8FA': ')', '4o4s': '/'}
string = 'gde1'
choices = f'({"|".join(mapping)})'
result = ''.join(mapping.get(s, s) for s in re.split(choices, string))
print(result)

This Code Is Supposed To Output ‘a’.

But It Gives Me This Error Message:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Traceback (most recent call last):
  File "c:\EDS\fatstrst.py", line 5, in <module>
    result = ''.join(mapping.get(s, s) for s in re.split(choices, string))
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.3312.0_x64__qbz5n2kfra8p0\lib\re.py", line 231, in split
    return _compile(pattern, flags).split(string, maxsplit)
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.3312.0_x64__qbz5n2kfra8p0\lib\re.py", line 304, in _compile
    p = sre_compile.compile(pattern, flags)
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.3312.0_x64__qbz5n2kfra8p0\lib\sre_compile.py", line 764, in compile
    p = sre_parse.parse(p, flags)
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.3312.0_x64__qbz5n2kfra8p0\lib\sre_parse.py", line 950, in parse
    p = _parse_sub(source, state, flags & SRE_FLAG_VERBOSE, 0)
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.3312.0_x64__qbz5n2kfra8p0\lib\sre_parse.py", line 443, in _parse_sub        
    itemsappend(_parse(source, state, verbose, nested + 1,
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.3312.0_x64__qbz5n2kfra8p0\lib\sre_parse.py", line 836, in _parse
    p = _parse_sub(source, state, sub_verbose, nested + 1)
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.3312.0_x64__qbz5n2kfra8p0\lib\sre_parse.py", line 443, in _parse_sub        
    itemsappend(_parse(source, state, verbose, nested + 1,
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.3312.0_x64__qbz5n2kfra8p0\lib\sre_parse.py", line 668, in _parse
    raise source.error("nothing to repeat",
re.error: nothing to repeat at position 33

But When I Shorten The mapping = To:

mapping = {'gde1': 'a', '4&TW': 'b', 'E#ZB': 'c', 'B0F&': 'd'}

It Works Fine And The Output Is a.

Does Anyone Have Any Idea On How To Make This Work? Any Help Would Be Greatly Appreciated.

>Solution :

In your mapping you have special regex characters, such as + that indicates that the previous character/group can be repeated.

Your error is due to strings like +yvf where there is nothing before the +. Even if there was no error, you would match the wrong things (e.g. Jc&$ would match Jc& only in the end of the string)

You need to escape those characters. An easy way is to use re.escape:

string = 'gde1'
choices = f'({"|".join(map(re.escape, mapping))})'
result = ''.join(mapping.get(s, s) for s in re.split(choices, string))
print(result)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading