How to get rid the new line
James T. Dennis
jadestar at idiom.com
Thu Jun 27 18:53:23 EDT 2002
SiverFish <occeanlinux at linuxmail.org> wrote:
> On Thu, 27 Jun 2002 14:00:22 +0000, Peter Hansen wrote:
>> Markus von Ehr wrote:
>>>
>>> f = open(filename, 'r')
>>> lines = f.readlines()
>>> line1 = lines[0] # exemplarily for first line line1 =
>>> line1[0:len(line1)-1]
>>>
>>> or:
>>> f = open(filename, 'r')
>>> lines = f.readlines()
>>> line1 = lines[0][0:len(line1)-1] # exemplarily for first line
>>
>> I believe this is unsafe. The final line may not be terminated with \n.
>>
>> -Peter
> Any method like chomp in perl
> I just one to cut read only the line that not empty into the list
Could try this:
def chomp(line):
if line[-1]=='\n':
line=line[:-1]
return line
(or variations thereon, to satisfy other semantic requirements).
More information about the Python-list
mailing list