[Tutor] newbie: Reading text file

Kent Johnson kent37 at tds.net
Fri Jun 1 12:24:50 CEST 2007


Grant Hagstrom wrote:
> Thanks for your help Alan.
> 
> I found that when I used the code, it did returne a list but it is 
> riddled with formatting characters.
> My question is, is it possible to strip out multiple characters at once?

Yes, just pass multiple characters to strip(), e.g.
line = line.strip('\r\n\t \'",')

I'm curious why you don't like the import solution. exec and execfile() 
could also work although all three require that the file contain trusted 
data.

Here are a couple of recipes that do almost what you want (you would 
still have to strip out the "jobs = " part:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/364469
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/281056

Kent


More information about the Tutor mailing list