Python Forum
Highlight/Underline a string | ValueError: zero length field name in format
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Highlight/Underline a string | ValueError: zero length field name in format
#1
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
Reply
#2
In python2.6 and earlier you need indices in the format specs

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


Possibly Related Threads…
Thread Author Replies Views Last Post
  string format challenge jfc 1 78 Yesterday, 09:45 AM
Last Post: Larz60+
  Print first day of the week as string in date format MyerzzD 2 280 Sep-29-2021, 06:43 AM
Last Post: MyerzzD
  string.format() suddenly causing errors with google drive API zwitrader 0 376 Jun-28-2021, 11:38 PM
Last Post: zwitrader
  String to Date format SAF 2 816 Apr-06-2021, 02:09 PM
Last Post: snippsat
  Bug ? when dataclass field name == field type Cyril 0 520 Oct-22-2020, 03:26 AM
Last Post: Cyril
  ValueError: time data 'None' does not match format '%Y-%m-%dT%H:%M:%S.%f' rajesh3383 4 4,520 Sep-03-2020, 08:22 PM
Last Post: buran
  ValueError: could not convert string to float: RandomCoder 3 2,343 Jul-27-2020, 07:38 AM
Last Post: ndc85430
  find a string in a field in MongoDB Leon79 2 862 Jul-19-2020, 09:20 PM
Last Post: menator01
  Receiving ValueError("bad input shape {0}".format(shape)) error SuryaCitizen 2 1,717 Jun-01-2020, 06:45 AM
Last Post: pyzyx3qwerty
  getting error ValueError: time data '' does not match format '%H:%M' srisrinu 2 3,055 Apr-09-2020, 11:12 AM
Last Post: srisrinu

Forum Jump:

User Panel Messages

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