Discussion on some Code Issues

subhabangalore at gmail.com subhabangalore at gmail.com
Sun Jul 8 07:42:13 CEST 2012


On Sunday, July 8, 2012 2:21:14 AM UTC+5:30, Dennis Lee Bieber wrote:
> On Sat, 7 Jul 2012 12:54:16 -0700 (PDT), subhabangalore at gmail.com
> declaimed the following in gmane.comp.python.general:
> 
> > But I am bit intrigued with another question,
> > 
> > suppose I say:
> >   file_open=open("/python32/doc1.txt","r")
> >   file=a1.read().lower()
> >   for line in file:
> >        line_word=line.split()
> > 
> > This works fine. But if I print it would be printed continuously.
> 
> 	"This works fine" -- Really?
> 
> 1)	Why are you storing data files in the install directory of your
> Python interpreter?
> 
> 2)	"a1" is undefined -- you should get an exception on that line which
> makes the following irrelevant; replacing "a1" with "file_open" leads
> to...
> 
> 3)	"file" is a) a predefined function in Python, which you have just
> shadowed and b) a poor name for a string containing the contents of a
> file
> 
> 4) 	"for line in file", since "file" is a string, will iterate over EACH
> CHARACTER, meaning (since there is nothing to split) that "line_word" is
> also just a single character.
> 
> 	for line in file.split("\n"):
> 
> will split the STRING into logical lines (assuming a new-line character
> splits the lines) and permit the subsequent split to pull out wordS
> ("line_word" is misleading, as to will contain a LIST of words from the
> line).
> 
> > I like to store in some variable,so that I may print line of my choice and manipulate them at my choice.
> > Is there any way out to this problem?
> > 
> > 
> > Regards,
> > Subhabrata Banerjee
> -- 
> 	Wulfraed                 Dennis Lee Bieber         AF6VN
>         wlfraed at ix.netcom.com    HTTP://wlfraed.home.netcom.com/

Thanks for pointing out the mistakes. Your points are right. So I am trying to revise it,

file_open=open("/python32/doc1.txt","r")
for line in file_open:
         line_word=line.split()
         print (line_word)

To store them the best way is to assign a blank list and append but is there any alternate
method for huge data it becomes tough as the list becomes huge if any way variables may be assigned.

Regards,
Subhabrata Banerjee.





More information about the Python-list mailing list