Re: how to delete "\n"

2010-07-12 Thread Benjamin Kaplan
On Mon, Jul 12, 2010 at 2:33 PM, Jia Hu  wrote:
> Thank you. It works now.
>
>  if I use 'print' to print the whole list, 'print' will add newline
> at the end of the list but not each item in the list.  right?
>
> For the code:
> for line in fileName:
>     line = line.rstrip('\n')
> I think this will affect 'fileName' because it assign the value to 'line' ?

No it does not. Python assignments never modify anything other than
the enclosing namespace.
"for line in fileName" reads a line from the file and creates a
string. That string resides in memory somewhere, who knows where. It
does not reside inside fileName- it is a separate object. That string
is then bound to the name "line" in the current namespace (the
module's namespace in this case). Variables in Python are just entries
in a dictionary. In fact, you can do globals()["line"] and it will
return the string associated with that name.

line.rstrip("\n") gets the object associated with the name "line" and
accesses the object with the name "rstrip" that's associated with the
object named "line". It then calls that object (in this case a
builtin_function_or_method object) with the string "\n" as a
parameter.

rstrip then generates a new string.
The line = (that new string that was just generated) associates the
new string with the name "line". The old string hasn't changed at all
(in fact, the object will get deleted almost immediately since it's
reference count just dropped to 0). And the file hasn't changed
either.

> But when I print fileName, "\n" still exists at each item in the list.
>
> Because each line in my txt file are numeric values. is there any other
> better way to get each
> numerical value at each line? for example using numpy or changing string
> list to numerical list?
>
> Thank you for help.
>
> On Mon, Jul 12, 2010 at 4:45 PM, Chris Rebert  wrote:
>>
>> On Mon, Jul 12, 2010 at 1:27 PM, Jia Hu  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
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to delete "\n"

2010-07-12 Thread python
Thomas,

> split() also splits at other whitespace.

Doh! Corrected version follows:

print ''.join( open( 'Direct_Irr.txt' ).read().splitlines() )
 
Broken out:
 
- open(): open file 
- read(): read its entire contents as one string
- splitlines(): 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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to delete "\n"

2010-07-12 Thread Thomas Jollans
On 07/12/2010 11:29 PM, pyt...@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

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to delete "\n"

2010-07-12 Thread Jia Hu
Thank you. It works now.

 if I use 'print' to print the whole list, 'print' will add newline
at the end of the list but not each item in the list.  right?

For the code:
for line in fileName:
line = line.rstrip('\n')
I think this will affect 'fileName' because it assign the value to 'line' ?
But when I print fileName, "\n" still exists at each item in the list.

Because each line in my txt file are numeric values. is there any other
better way to get each
numerical value at each line? for example using numpy or changing string
list to numerical list?

Thank you for help.

On Mon, Jul 12, 2010 at 4:45 PM, Chris Rebert  wrote:

> On Mon, Jul 12, 2010 at 1:27 PM, Jia Hu  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
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to delete "\n"

2010-07-12 Thread python
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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to delete "\n"

2010-07-12 Thread Chris Rebert
On Mon, Jul 12, 2010 at 1:27 PM, Jia Hu  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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to delete "\n"

2010-07-12 Thread Ian Kelly
On Mon, Jul 12, 2010 at 2:27 PM, Jia Hu  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)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to delete "\n"

2010-07-12 Thread Matteo Landi
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  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/
-- 
http://mail.python.org/mailman/listinfo/python-list