Python Forum

Full Version: Highlight/Underline a string | ValueError: zero length field name in format
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
In python2.6 and earlier you need indices in the format specs

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