I have given it all I got: a week trying to open, read, write, split, append, 
convert to integers and then manipulate the data.  I have attached the file I 
want to pull the names and numbers from.  I'll cut and paste it incase that 
doesn't work and then include a sample of the python shell code.  

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

# and now my newbie attempt:
# I can get this far in approach #1
>>> filename = "z:/Geog482/egund919/Program1/BOJ.txt"
>>> myfile = open(filename,"r")
>>> newfile = "z:/Geog482/egund919/Program1/BOJ_B.txt"
>>> mynewfile = open(newfile,"w")
>>> while True:
 line = myfile.readline()
 print line
 mynewfile.write(line)
 if not line: break
 
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


In approach number 2: trying to append, convert to integer, but the list comes 
up empty:

>>> 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)
 
>>> type(data_value)
<type 'list'>
>>> print(data_value)
[]
>>> print (line)


All I get is errors from here on out.....


LN
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
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Reply via email to