Python Forum
Highlight/Underline a string | ValueError: zero length field name in format - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Highlight/Underline a string | ValueError: zero length field name in format (/thread-19467.html)



Highlight/Underline a string | ValueError: zero length field name in format - searching1 - Jul-01-2019

Hi, I'm building a python script which put an underline on a matched string on my regex pattern. Issue is when migrated to 2.6 I'm getting the below traceback. What would be the correct way to build this correctly?

finalout = re.sub(r'>.*(?=.*)', lambda m: '\x1b[4m{}\x1b[0m'.format(m.group()),compout)
print finalout
Error:
Traceback (most recent call last): File "compcon.py", line 313, in <module> main() File "compcon.py", line 302, in main finalout = re.sub(r'>.*(?=.*)', lambda m: '\x1b[4m{}\x1b[0m'.format(m.group()),compout) File "/usr/lib64/python2.6/re.py", line 151, in sub return _compile(pattern, 0).sub(repl, string, count) File "compcon.py", line 302, in <lambda> finalout = re.sub(r'>.*(?=.*)', lambda m: '\x1b[4m{}\x1b[0m'.format(m.group()),compout) ValueError: zero length field name in format



RE: Highlight/Underline a string | ValueError: zero length field name in format - metulburr - Jul-01-2019

In python2.6 and earlier you need indices in the format specs

try:
'\x1b[4m{0}\x1b[0m'.format(m.group())