Skip to content Skip to sidebar Skip to footer

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 Th

Solution 1:

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

info = infl.readline().rstrip()

Solution 2:

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

Solution 3:

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()
whileTrue:
    #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)...

Post a Comment for "Trouble Concatenating Two Strings"