how to delete "\n"

Ian Kelly ian.g.kelly at gmail.com
Mon Jul 12 16:38:49 EDT 2010


On Mon, Jul 12, 2010 at 2:27 PM, Jia Hu <hujia06 at gmail.com> wrote:
> Hi, I just want to delete "\n" at each line. My operating system is ubuntu
> 9.1. The code is as follows
>
> #!/usr/bin/python
> import string
> fileName=open('Direct_Irr.txt', 'r') # read file
> directIrr = fileName.readlines()
> fileName.close()
> for line in directIrr:
>        line.rstrip('\n')
> print directIrr
>
> But I found there is still "\n" . Could someone help me why it is not
> correct?

The print statement automatically adds a newline to each line.  If you
want the lines written verbatim, use sys.stdout.write(line)



More information about the Python-list mailing list