Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python multiline string format

Tags:

python

I'm trying to have a script to generate some makefiles for me. I want to format this multiline string, but I'm getting a strange error.

Code:

make_content = """ PCC = pgcc 
%(bench)_serial: src/main.c src/%(bench)_serial.c ../common/util.c
\t$(PCC) $(ACCFLAGS) -o bin/%(bench)_serial src/main.c src/%(bench)_serial.c

clean:
\trm -rf *.o *.oo bin/*""" % {'bench':'umpalumpa'}

Error:

Traceback (most recent call last):
  File "./new_bench.py", line 27, in <module>
    \trm -rf *.o *.oo bin/*""" % {'bench':'umpalumpa'}
ValueError: unsupported format character '_' (0x5f) at index 21

Any ideas?

Notes: this is a truncated version of the makefile, no comments on that. Notes[2]: 'umpalumpa' is a placeholder to make sure it's a string. It'll be something real one day.

Edit: I'm using python 2.7

like image 655
leo Avatar asked Dec 06 '25 09:12

leo


1 Answers

As you have already got the answer as to why that didn't work, a better way and also recommended to use if format function (If you are using `Python 2.6+): -

"src/{bench}_serial.c".format(bench='umpalumpa')

So, for your string, it becomes: -

ake_content = """ PCC = pgcc 
{bench}_serial: src/main.c src/{bench}_serial.c ../common/util.c
\t$(PCC) $(ACCFLAGS) -o bin/{bench}_serial src/main.c src/{bench}_serial.c

clean:
\trm -rf *.o *.oo bin/*""".format(bench='umpalumpa')
like image 92
Rohit Jain Avatar answered Dec 08 '25 22:12

Rohit Jain