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
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')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With