Flux Capacitor
Flux Capacitor

Reputation: 1231

Trouble concatenating two strings

I am having trouble concatenating two strings. This is my code:

info = infl.readline()
while True:
    line = infl.readline()
    outfl.write(info + line)
    print info + line

The trouble is that the output appears on two different lines. For example, output text looks like this:

490250633800 802788.0 953598.2
802781.968872 953674.839355 193.811523 1 0.126805 -999.000000 -999.000000 -999.000000

I want both strings on the same line.

Upvotes: 1

Views: 1876

Answers (3)

cwallenpoole
cwallenpoole

Reputation: 81988

readline will return a "\n" at the end of the string 99.99% of the time. You can get around this by calling rstrip on the result.

info = infl.readline().rstip()
while True:
    #put it both places!
    line = infl.readline().rstip()
    outfl.write(info + line)
    print info + line

readline's docs:

Read one entire line from the file. A trailing newline character is kept in the string (but may be absent when a file ends with an incomplete line)...

Upvotes: 1

Cydonia7
Cydonia7

Reputation: 3826

You should remove line breaks in the line and info variables like this : line=line.replace("\n","")

Upvotes: 1

unutbu
unutbu

Reputation: 879083

There must be a '\n' character at the end of info. You can remove it with:

info = infl.readline().rstrip()

Upvotes: 7

Related Questions