tags:

views:

63

answers:

3

Don't flame but I'm still a python newbie. I need to suppress the carriage return after I display a variable.

data = """
[virtual_machines: %s]
    \taddress %s.domain.com
""" % (line, line)

fg = file('munin.txt', 'a')
fg.write(stuff)

When printing this out, it creates a new line after the variable gets printed. I tried using %r but that displays the "\n" code.

Edit: I'm actually trying to write it into another file.

+2  A: 
data = """
[virtual_machines: %s]
    \taddress %s.domain.com""" % (line, line)
Jared Updike
+2  A: 

How are you printing this out?

If you are using print, simply append a trailing comma:

print data,

which will suppress the newline.

Yann Ramin
Actually, writing it into another file, sorry about that. I've updated my post.
luckytaxi
+3  A: 

If I understand your question correctly, you are seeing a new-line after the %s. It looks like line may have a newline in it, in which case you can do line.strip() to remove all whitespace around it:

... " % (line.strip(), line.strip())

or

 ... " % (line.strip(), ) * 2

If you are seeing an unwanted newline at the end of the whole thing, it is because you have a newline in your multi-line string and should refer to Jared Updike's answer.

orangeoctopus
that did it! perfect, thx.
luckytaxi
Kind of overkill ;)
Yann Ramin