2

hi i want to write the newline character to an output file so here is my code:

a=['\n:001000', '\r:10111', ' :000', '!:01101111101', '":0010011', "':0010010110", '(:00100101111110110', '):00100101111110111', ',:101100', '-:1011011011', '.:0100100', '0:011011111001101', '1:0110111110010', '2:1011011000111', '3:011011111001110']

text_file = open("Output.txt", "wb")
    for t in a:
        text_file.write(t+" ")

but my output is not what i expected:

:001000  :10111   :000  !:01101111101  ":0010011  ':0010010110  (:00100101111110110  ):00100101111110111  ,:101100  -:1011011011  .:0100100  0:011011111001101  1:0110111110010  2:1011011000111  3:011011111001110  

does anyone know how to actually write the newline character to the output ???

i want something like eg: \n:001000 \r:10111 etc

1
  • Writing the newline character is as simple as appending "\n" or '\n' to the print statement as @Michel pointed out. Furthermore, the '\r' carriage return works, look at the file in a text editor! Could you please try and more accurately phrase your intentions? What was unexpected about the output you got? Is it the same in the file? What should it have looked like? Commented May 1, 2013 at 8:43

2 Answers 2

1

You are printing those characters, just look at the repr

>>> a=['\n:001000 ', '\r:10111 ', ' :000 ', '!:01101111101 ', '":0010011 ', "':0010010110 ", '(:00100101111110110 ', '):00100101111110111 ', ',:101100 ', '-:1011011011 ', '.:0100100 ', '0:011011111001101 ', '1:0110111110010 ', '2:1011011000111 ', '3:011011111001110 ']
>>> with open("Output.txt", "wb") as f:
        for t in a:
            f.write(t + " ")


>>> with open("Output.txt", "rb") as f:
        print repr(f.read()) # representation


'\n:001000  \r:10111   :000  !:01101111101  ":0010011  \':0010010110  (:00100101111110110  ):00100101111110111  ,:101100  -:1011011011  .:0100100  0:011011111001101  1:0110111110010  2:1011011000111  3:011011111001110  '

Or maybe you are asking to escape those characters to print them raw:

>>> with open("Output.txt", "wb") as f:
        for t in a:
            f.write((t + " ").encode('string_escape'))


>>> with open("Output.txt", "rb") as f:
        print f.read()


\n:001000  \r:10111   :000  !:01101111101  ":0010011  \':0010010110  (:00100101111110110  ):00100101111110111  ,:101100  -:1011011011  .:0100100  0:011011111001101  1:0110111110010  2:1011011000111  3:011011111001110  
Sign up to request clarification or add additional context in comments.

2 Comments

i just wish to read the first line as there are other things after that first line that i dont put it on the question cause i just stuck at that question
@TommyNgo To read the first line change .read to .readline
0

The new line character is '\n'

3 Comments

yes i wanna write it out to the output file but that is the result that i get.
But you write " " instead of "\n"
the space is just the space to separate \n:001000 from \n:001000

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.