5
import StringIO
output = StringIO.StringIO()   
output.write('this is the first line\n the new line should come here')
a = output.getvalue()
output.close()
print a 

But the new line character \n does not get printed as a new line.

How do I get a newline?

Radek Simko
  • 15,886
  • 17
  • 69
  • 107
user3676102
  • 51
  • 1
  • 2
  • Works on my computer. – Hans Then May 26 '14 at 11:20
  • What OS do you have? What you show shall work well. On my Ubuntu it prints new line. You might use MS Windows, if it really does not work there, I would try `\n\r`. – Jan Vlcinsky May 26 '14 at 11:23
  • Check this post, is about make line break in mac osx: [How do i create line break in terminal](http://stackoverflow.com/questions/5190030/how-do-i-create-a-line-break-in-terminal) – AlvaroAV May 26 '14 at 11:30
  • Python 2.7.3 (default, Apr 10 2013, 06:20:15) [GCC 4.6.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import StringIO >>> output=StringIO.StringIO() >>> output.write('This is a new\nline and I \n amd') >>> a=output.getvalue() >>> a 'This is a new\nline and I \n amd' >>> – user3676102 May 26 '14 at 11:32
  • 3
    @user3676102; rather than typing `a`; use `print(a)` as per your question. Do the newlines display correctly if you print? Your comment above is the way I'd expect Python to work. – Ben May 26 '14 at 11:40
  • works too on my Windows 8.1 x64 machine, Python 2.7.6 – sb9 May 26 '14 at 11:43
  • 3
    use `os.linesep` as the line separator character(s) – andrean May 26 '14 at 11:43

0 Answers0