Reputation: 3
I am trying to print from a dictionary to populate another script. I keep getting "unexpected character after line continuation character". I dont know why
mydic = {"printer1": "printserver1", "printer2": "printserver2"}
for key in mydic.keys():
print ("For /F \"Tokens=4 delims=\\\" %%I In ('reg query HKCU\\Printers\\Connections ^|find /I \"" + key + "\"') Do If \"%%I\"==\",,"+ mydic[key] +"," + key + "\" goto :REMOVE" + \n + "goto :SKIP" + \n + ":REMOVE" + \n + "RUNDLL32 printui.dll,PrintUIEntry /n \\\\"+ mydic[key] +"\\" + key + " /dn" + \n + ":SKIP")
Upvotes: 0
Views: 99
Reputation: 1121416
Make the \n
newlines part of the string:
print ("For /F \"Tokens=4 delims=\\\" %%I In ('reg query HKCU\\Printers\\Connections ^|find /I \"" +
key + "\"') Do If \"%%I\"==\",," + mydic[key] + "," + key +
"\" goto :REMOVE\ngoto :SKIP\n:REMOVERUNDLL32 printui.dll,PrintUIEntry /n \\\" +
mydic[key] +"\\" + key + " /dn\n:SKIP")
You really want to use some string formatting here, together with a triple-quoted raw string to cut down on all those backslashes:
template = r"""For /F "Tokens=4 delims=\" %%I In ('reg query HKCU\Printers\Connections ^|find /I "{0}"') Do If "%%I"==",,"{1},{0}" goto :REMOVE
goto :SKIP
:REMOVERUNDLL32 printui.dll,PrintUIEntry /n \{1}\{0} /dn
:SKIP"""
for key in mydic:
print(template.format(key, mydic[key]))
Upvotes: 2
Reputation: 142116
Simplify this so it's not all one line, more readable and newlines are automatically part of the string by using multi-line strings:
mydic = {"printer1": "printserver1", "printer2": "printserver2"}
template = r"""
FOR /F in blah blah blah
do something with {key}
with the value of {value}
blah blah blah
END FOR
"""
for key, value in mydic.iteritems():
print template.format(key=key, value=value)
Upvotes: 8