1

I am trying to use f.write i am not sure its a best way to do but i saw couple of example. here what i am trying to do, I have a file which contain following lines.

parallel (
{
ignore(FAILURE) {
                          build( "Deploy",  BUILDFILE: "/path/to/build.xml", WARFILE: "http://www.example.com/repo/file.war", STUDY: "EXAMPLE", BUG: "007" )
}},

In above file you can see BUILDFILE, WARFILE, STUDY and BUG field. I want to to edit them using script in place of manually edit. I don't under stand how do i use variable in f.write() function. following what i am trying to do

BF = raw_input("Enter BUILDFILE name:")
WF = raw_input("Enter WARFILE name:")
STUDY = raw_input("Enter STUDY name:")
BUG = raw_input("Enter BUG name:")
f = open("myfile", "w")
data = """parallel (
    {
    ignore(FAILURE) {
                              build( "Deploy",  BUILDFILE: "BF", WARFILE: "WF", STUDY: "STUDY", BUG: "BUG" )
    }},
f.write(data)
f.close()

when i am running this code it take my input and put in those specified field but some how its not working.. i don't know how to use f.write to take my variable and place in those fields. if there is a another way please let me know..

EDIT

I have modified script as per users suggested but still getting error, I am missing something ???

#!/usr/bin/python

import sys

BF = raw_input("Enter BUILDFILE name:")
WF = raw_input("Enter WARFILE name:")
STUDY = raw_input("Enter STUDY name:")
BUG = raw_input("Enter BUG name:")
f = open("myfile", "w")

data = """parallel (
{
ignore(FAILURE) {
                          build( "Deploy",  BUILDFILE: "{BF}", WARFILE: "{WF}", STUDY: "{STUDY}", BUG: "{BUG}" )
}},""".format(**locals())

f.write(data)
f.close()

following error i am getting

Traceback (most recent call last):
  File "./sched.py", line 18, in <module>
    }},""".format(**locals())
KeyError: '\nignore(FAILURE) {\n                          build( "Deploy",  BUILDFILE'
Satish
  • 16,544
  • 29
  • 93
  • 149

2 Answers2

5

Try string formatting with the str.format method. In this method, you can use a {Name} token in the string as a marker to be replaced. Also note that you need to close you triple quoted string.

data = """parallel (
{{
ignore(FAILURE) {{
                          build( "Deploy",  BUILDFILE: "{BF}", WARFILE: "{WF}", STUDY: "{STUDY}", BUG: "{BUG}" )
}}}},""".format(BUG=BUG,BF=BF,WF=WF,STUDY=STUDY)

Since you're also using literal { in your replacement string, you need to double the { that you want to escape.


For the lazy,

data = """parallel (
{{
ignore(FAILURE) {{
                          build( "Deploy",  BUILDFILE: "{BF}", WARFILE: "{WF}", STUDY: "{STUDY}", BUG: "{BUG}" )
}}}},""".format(**locals())

would also work but I wouldn't advise it as it isn't quite as clear.

Community
  • 1
  • 1
mgilson
  • 300,191
  • 65
  • 633
  • 696
  • I am getting this error: `File "./sched.py", line 13 }},""".format(BUG=BUG,BF=BF,WF=WF,BUG=BUG) SyntaxError: keyword argument repeated` – Satish May 16 '13 at 19:59
  • @Satish remove the last `BUG=BUG` – Jon Clements May 16 '13 at 20:00
  • Still i am getting error i guess i am missing some module, do you know which module i need to import for formate ? – Satish May 16 '13 at 20:05
  • Here is my error:: `Traceback (most recent call last): File "./sched.py", line 13, in }},""".format(BF=BF,WF=WF,STUDY=STUDY,BUG=BUG) KeyError: '\nignore(FAILURE) {\n build( "Deploy", BUILDFILE' ` – Satish May 16 '13 at 20:06
  • 2
    @Satish -- Thanks. It's hard to believe so many people upvoted a bad answer. It turns out that you need to double the curly braces you aren't using to escape them. – mgilson May 16 '13 at 20:22
2

The writeis not the problem here. You're not placing your variables in your data. (And you're not closing the string with """ either)

Try:

data = """parallel (
{{
    ignore(FAILURE) {{
      build( "Deploy",  BUILDFILE: "{0}", WARFILE: "{1}", STUDY:"{2}", BUG: "{3}" )
}}}}
"""
f.write(data.format(BF, WF, STUDY, BUG))

Note that you have to quote the brackets by writing {{ and }} (or python will complain, since it thinks it's the beginning of a format field) And have a look at Python's format function

MartinStettner
  • 28,719
  • 15
  • 79
  • 106