2

This is to take text from a file and combine with a string to print to a new file for a combined result

file = open('/home/user/facts', 'r')
result = open('/home/user/result.txt', 'a')
i = 1
for line in file:
    print >>result, "fact_text[%d] = \"%s\";"% (i, line)
    i += 1

For some reason the "; is showing up on a separate line, and I do not know why. Thanks in advance.

2 Answers 2

6

When reading lines from a file using for line in file the resulting string contains a newline character. You can strip it off using line.strip(). So your print statement becomes:

print >>result, "fact_text[%d] = \"%s\";" % (i, line.strip())
Sign up to request clarification or add additional context in comments.

Comments

1

Because line contains a newline character at the end. You could trim it by doing line[:-1] or -2, depending on if you have DOS or Unix line endings

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.