On Sun, Jul 20, 2014 at 8:40 AM, LN A-go-go
<lnart...@yahoo.com.dmarc.invalid> wrote:

>
> States OJ
> AK 36
> AL 39
> AR 39
> AZ 45
> CA 61
> CO 54
> CT 61
> DC 93
> DE 62
> FL 51
> GA 47
> HI 72
> IA 54
> ID 36
> IL 62
> IN 50
> KS 41
> KY 41
> LA 40
> MA 62
> MD 62
> ME 58
> MI 57
> MN 54
> MO 49
> MS 43
> MT 47
> NC 50
> ND 45
> NE 41
> NH 54
> NJ 57
> NM 57
> NV 55
> NY 62
> OH 51
> OK 34
> OR 57
> PA 55
> RI 63
> SC 45
> SD 45
> TN 42
> TX 44
> UT 34
> VA 53
> VT 67
> WA 58
> WI 56
> WV 43
> WY 33
>
> >>> myfile.close()
> >>> infile = open('C:/Python27/egund919/Program1/BOJ_B.txt','r')
> >>> import string
> >>> state_name = []
> >>> data_value = []
> >>> counter = 0
> >>> x = 0
> >>> line = infile.readline()
> >>> while True:
>               line = infile.readline()
>               if not line: break
>               counter = counter + 1
>               States, OJ = string.split(line)
>               XSTATES = str(States)
>               XNUM = int(OJ)
>               state_name.append(XSTATES)
>               data_value.append(XNUM)
>
> >>> len(counter)
> Traceback (most recent call last):
>   File "<pyshell#29>", line 1, in <module>
>     len(counter)
> TypeError: object of type 'int' has no len()
> >>> counter.count
> Traceback (most recent call last):
>   File "<pyshell#30>", line 1, in <module>
>     counter.count
> AttributeError: 'int' object has no attribute 'count'
> >>> len(state_name)
> 0
> >>> list.state_name
> Traceback (most recent call last):
>   File "<pyshell#32>", line 1, in <module>
>     list.state_name
> AttributeError: type object 'list' has no attribute 'state_name'
>

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.

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.

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.

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.

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])
        line = infile.readline()
print(state_name)
print(data_value)
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Reply via email to