another question

DavidW Blaschke dwblas at yahoo.com
Thu Jun 5 21:00:46 EDT 2003


Replace the "|" with a space:
    new_rec = string.replace( line, "|", " " )
Might as well get rid of newlines also
    new_rec = string.replace( new_rec, "\n", " " )
Then string.strip(new_rec)
 
If you want to use string.split(new_rec), you might
want to split on the "|" and then replace it with a
space.

D.W.

> --- In python-list at yahoogroups.com, "Leeds, Mark"
> <mleeds at m...> wrote:
> > i'm sorry to bother everyone again :
> >  
> > i am a novice at python and i am
> > reading a file into memory
> > using the command
> > file.readlines.
> >  
> > the file has spaces between the elements but the
> delimiter that 
> really
> > separates
> > them  is a "|".
> >  
> > so i wrote the subroutine below.
> >  
> > def getData(file):
> >  
> >     anyFile=open(file,'r')
> >     anyStuff=anyfile.readlines()
> >     anyFile.close
> >  
> >     anydata=[]
> >  
> >     for line in anyStuff:
> >         if not line:
> >             continue
> >         line=string.strip(line)
> >         anydata.append(string.split(line))
> >     return(anydata)
> >  
> > 
>
#---------------------------------------------------------------------
> --
> > ------------------------------------------
> >  
> > in the main, i have
> >  
> > textfile=("xxx.dat")
> > datafile=getData(textfile))
> > print(datafile)
> >  
> > the problem is that i don't know how to get rid of
> the
> > "|" characters so these show up in the output.
> >  
> > if i use a parameter in string.split, then i will
> end up with spaces
> > which i don't want either.
> >  
> > i guess the question is how to get rid of both the
> spaces
> > and the "|" characters. 
> >  
> >                                                
> thanks.
> 


__________________________________
Do you Yahoo!?
The New Yahoo! Search - Faster. Easier. Bingo.
http://search.yahoo.com





More information about the Python-list mailing list