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 g开发者_JAVA百科ets printed. I tried using %r but that displays the "\n" code.
Edit: I'm actually trying to write it into another file.
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.
data = """
[virtual_machines: %s]
\taddress %s.domain.com""" % (line, line)
How are you printing this out?
If you are using print
, simply append a trailing comma:
print data,
which will suppress the newline.
精彩评论