[Tutor] (no subject)

Wolfgang Maier wolfgang.maier at biologie.uni-freiburg.de
Sun Jul 20 23:47:25 CEST 2014


On 20.07.2014 22:37, Marc Tompkins wrote:
>
> First of all, I would take advantage of the "with" construction, like so:
>
> with open('C:/Python27/egund919/Program1/BOJ_B.txt','r') as infile:
>      #do stuff with infile
>
> When the block of code inside of the "with" section finishes, Python
> will take care of closing the file for you - even if your program
> encountered an error.  MAJOR advantage over doing it yourself.
>

Yes, definitely, as you have experienced yourself now.

> Second, there's no need to import "string" anymore; all of the
> functions defined in that module are native methods of strings.
> "line" is a string, so you can just do "line.split()".  split()
> returns a list; you can get the first item in that list with
> line.split()[0], the second item with line.split()[1], etc.
>

I fully agree on the string method part. As for the use of 
line.split()[0] and [1], see my comment in the code below.

> Third, free your mind from the shackles of counting how many objects
> are in a list and using that counter as the index of your loops - this
> is not the Python way!  Instead, let Python take care of the counting
> and indexing for you.
>

True (see the earlier suggestion of using enumerate()), but I'm sure 
you'll learn this over time.

> Fourth (and here I differ from some of the others on this list,
> notably Alan G) - I don't like the "while True: / break" paradigm.
> Your mileage may vary, but I find it makes it harder to understand
> what the program is actually trying to achieve.  In your case, you
> read a line from the file, then check whether it contains anything; I
> like to move the readline() to the bottom of the loop.

In my opinion, this is less readable than the original code. I know that 
some people (usually coming from C or other languages with more 
expressive while loops) don't like it, but there's nothing wrong, in 
general, with using while True with break. It's perfectly pythonic.

Direct use of the file object in a for loop has been suggested already 
and THAT's more readable.

>
> Having said all that, here's my suggested version:
>
> state_name = []
> data_value = []
> with open('C:/Python27/egund919/Program1/BOJ_B.txt','r') as infile:
>      line = infile.readline()  # Doing this twice because you
> apparently want to discard the first line...?
>      line = infile.readline()
>      while line:
>          state_name.append(line.split()[0])
>          data_value.append(line.split()[1])

Rewriting the original

            States, OJ = string.split(line)

as

            states, oj = line.split()

is better (and it's remarkable that you used it as a beginner). This 
line unpacks the elements of the list returned by the split method into 
the two variables on the left side.
Besides avoiding repetitive calls to line.split(), it may also help you 
discover problems with your input file format. If there are more than 
two elements in the list, you'll get an error, so you'll know there was 
something in the file you didn't expect. With the line.split()[0] / [1] 
approach, potential other elements in the list (i.e. unexpected line 
content) will be ignored silently.
Once you've learnt that errors in Python are nothing evil, but there to 
help you and once you've read about try/except to catch them, you'll 
appreciate the advantage of the first version.

>          line = infile.readline()

I don't see why repeating infile.readline() three times should be more 
elegant than a nice and clean while True / break.

> print(state_name)
> print(data_value)




More information about the Tutor mailing list