Prev: GUIs (was: grailbrowser now running under python 2.5 (probably above too))
Next: multitask http server (single-process multi-connection HTTP server)
From: Matteo Landi on 12 Jul 2010 16:34 I hope this could help: >>> f = open('powersave.sh') >>> map(lambda s: s.strip(), f.readlines()) ['echo 1 > /sys/module/snd_hda_intel/parameters/power_save', 'echo min_power > /sys/class/scsi_host/host0/link_power_management_policy', 'echo 1 > /sys/module/snd_hda_intel/parameters/power_save'] I know for sure someone else will address you to other better solutions :) On Mon, Jul 12, 2010 at 10:27 PM, Jia Hu <hujia06(a)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? > > Thank you > > > -- > http://mail.python.org/mailman/listinfo/python-list > > -- Matteo Landi http://www.matteolandi.net/
From: Ian Kelly on 12 Jul 2010 16:38 On Mon, Jul 12, 2010 at 2:27 PM, Jia Hu <hujia06(a)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)
From: Chris Rebert on 12 Jul 2010 16:45 On Mon, Jul 12, 2010 at 1:27 PM, Jia Hu <hujia06(a)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? ..rstrip() returns a *new* string without trailing whitespace (which you are currently then throwing away); it does *not* modify string objects in-place. Python strings objects are entirely immutable and unmodifiable; all operations on them merely produce /new/ strings. Assuming you still want to use .readlines(), you'd do: directIrr = fileName.readlines() fileName.close() directIrr = [line.rstrip('\n') for line in directIrr] print directIrr For how third line works, google "python list comprehensions". Cheers, Chris -- http://blog.rebertia.com
From: python on 12 Jul 2010 17:29 Jia, print ''.join( open( 'Direct_Irr.txt' ).read().split() ) Broken out: - open(): open file - read(): read its entire contents as one string - split(): split the contents into a list of lines (splits lines at \n; does not include \n in split values) - ''.join(): join list of lines with an empty char Malcolm
From: Thomas Jollans on 12 Jul 2010 17:36
On 07/12/2010 11:29 PM, python(a)bdurham.com wrote: > Jia, > > print ''.join( open( 'Direct_Irr.txt' ).read().split() ) > > Broken out: > > - open(): open file > - read(): read its entire contents as one string > - split(): split the contents into a list of lines > (splits lines at \n; does not include \n in split values) also splits at other whitespace. > - ''.join(): join list of lines with an empty char > > Malcolm |