Newbie Question
Art Haas
arthur.haas at westgeo.com
Thu Apr 27 18:35:46 EDT 2000
"Arnaldo Riquelme" <javanet at dynacap.com> writes:
> Suppose I have a file name tick.tkr that looks like this
>
> SUNW.
> MFST.
> FOO.
> SPAM.
> MOO.
> End_Of_File
>
> I want to read the file into a list
>
> myfile = open("tick.tkr", "r")
> mylist = myfile.readlines()
>
> Now I have mylist with the contents of tick.tkr. "mylist" looks like this
> ['SUNW.\012', 'MSFT.\012', ..........'End_of_File/012']
>
> How do I read the file into a list without getting the newline character, so
> I can accomplish this
>
> for tic in mylist:
> #create file tic.prn
>
> I know the other P language has something like 'chop' or something like
> that, but I'm not going there.
In the other `P' language you'd probably use chomp() ...
my $file = 'tick.tkr';
open(TICK,"<$file") || die "Can't open '$file'! $!\n"
chomp(my @lines = <TICK>);
close(TICK);
In python ...
>>> import string
>>> myfile = open("tick.tkr", "r")
>>> mylist = map(string.strip, myfiles.readlines())
>>> myfile.close()
>>> mylist
['SUNW.', 'MFST.', 'FOO.', 'SPAM.', 'MOO.']
>>>
--
###############################
# Art Haas
# (713) 689-2417
###############################
More information about the Python-list
mailing list