Suppressing newline writing to file after variable

Noah Hall enalicho at gmail.com
Tue Jun 28 12:18:28 EDT 2011


On Tue, Jun 28, 2011 at 5:05 PM, Ellerbee, Edward <EEllerbee at bbandt.com> wrote:
> Hi all, newbie question here. I'm using python 2.7. I've built my first
> program to pull some info off the web, process it, and build dialpeers for a
> cisco router. I have 2 problems - the first is the formatting of printing
> the gathered information to a file. It seems to be inserting a new line
> after the variable is written. I've searched the web, but unsure of which
> method could fix this issue.
>
> Here is my code snippet:
>
> count=0
> o = open('dialpeers.txt', 'w')
> for line in open('final.txt', 'r'):
>     figureDpn = count + 1000

>     dpn = str(figureDpn)
>     label = "dial-peer voice " + dpn
>     o.write(label)
>     o.write('\n')
>     destpatt = "destination-pattern " + line + "...."

Try line.rstrip() instead. It'll remove all newlines. Also, I suggest
you use string formatting, for example,
>>>destpatt = "destination-pattern %s...." % line.rstrip()



More information about the Python-list mailing list