[Tutor] Re: Tutor digest, Vol 1 #1246 - 19 msgs

Aedin Culhane Aedin@chah.ucc.ie
Wed, 5 Dec 2001 16:16:28 +0000 (GMT)


Dear Phelim,
I'm also a newbie, so got very excited when I thought I could actually
help with your query.. so I had to respond.. exciting times for me.

Have you tried to string.rstrip() to remove trailing space from the
string you are reading in?  

When reading files, I find it easier to read all of the file:
 
file = open(inputfile, 'r')
data = file.readlines()
list =[]
for line in data:
   list.append(line.split())  # add each line in file to list.

Hope this helps
Aedin

--------------------------------
Aedin Culhane
Bioinformatics Group, University College Cork, Cork, Ireland



> --__--__--
> 
> Message: 19
> Date: Wed, 05 Dec 2001 07:40:15 -0500
> From: Andrei Kulakov <sill@optonline.net>
> Subject: Re: [Tutor] Blank line added after reading a line from a file
> To: "'tutor@python.org'" <tutor@python.org>
> Reply-to: ak@silmarill.org
> 
> On Wed, Dec 05, 2001 at 12:29:55PM +0000, Kelly, Phelim wrote:
> > (I'll try this again!)
> > 
> > Hello, 
> > I have a small probelm, hope you can help. I'm reading text from an external
> > file into a python program as follows: 
> > 
> > ---------------------
> > filename = raw_input("Enter the filename to read from: "); 
> > p=0
> > in_file = open(filename,"r")
> > while p < 4:
> >    text = in_file.readline()
> >    list[p]=text #list is an array which stores the content of each line read
> > 
> >                 #in.
> >    p = p + 1
> > in_file.close()
> 
> This would be easier done as:
> 
> in_file = open(filename)
> text = inf_file.readlines()
> lines_1_4 = text[:4]
> 
> That is, assuming file will comfortably fit in memory, which is the case
> with most text files.
> 
> > ---------------------------
> > 
> > The problem I have is that the text that is stored in variable 'text' isn't
> > simply the contents of one line of the file, another blank line is appended
> > onto the end, which causes problems for the rest of the program, so instead
> > of this, 
> > --------------------- 
> > line read in from file 
> > -------------------- 
> > 
> > I get this, 
> > 
> > ------------------- 
> > line read in from file 
> > 
> > ------------------- 
> > Can anyone tell me the command used to get rid of the extra blank line added
> > on? 
> 
> I think this has to do with how you print them out... how do you do
> that?
> 
> > Thanks in advance, 
> > Phelim.