-1

I am trying to format a string to pass to os.popen() so I can do some parsing in awk. I'm using python 2.7.10. When I try to format the string, python appears to think that I'm trying to assign a new key/value pair to a dictionary.

>>> path="xyz"
>>> string="awk {}".format(path)   #<--- This works as expected!
>>> string
'awk xyz'
>>> 
>>> string="awk 'BEGIN{FS=\"\t\"}{print $1}' {}"  #<--- This works, but not what I want
>>> string
'awk \'BEGIN{FS="\t"}{print $1}\' {}'
>>> 
>>> string="awk 'BEGIN{FS=\"\t\"}{print $1}' {}".format(path)  #<--- This fails b/c Python thinks I'm trying to do something with dictionaries?
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 'FS="\t"'

How do I format my string in the case where there are {} that are not intended to get formatted and ones that are?

irritable_phd_syndrome
  • 4,631
  • 3
  • 32
  • 60

1 Answers1

1

https://docs.python.org/2/library/string.html#format-string-syntax

If you need to include a brace character in the literal text, it can be escaped by doubling: {{ and }}.

Jacob Krall
  • 28,341
  • 6
  • 66
  • 76