writing results to array
Chris
cwitts at gmail.com
Tue Dec 4 03:06:28 EST 2007
On Dec 3, 10:45 pm, Bevan Jenkins <beva... at gmail.com> wrote:
> Hello,
>
> I have recently discovered the python language and am having a lot of
> fun getting head around the basics of it.
> However, I have run into a stumbling block that I have not been able
> to overcome, so I thought I would ask for help.
> <Overview>
> I am trying to import a text file that has the following format:
> 02/01/2000 @ 00:00:00 0.983896 Q10 T2
> 03/01/2000 @ 00:00:00 0.557377 Q10 T2
> 04/01/2000 @ 00:00:00 0.508871 Q10 T2
> 05/01/2000 @ 00:00:00 0.583196 Q10 T2
> 06/01/2000 @ 00:00:00 0.518281 Q10 T2
> when there is missing data:
> 12/09/2000 @ 00:00:00 Q151 T2
> 13/09/2000 @ 00:00:00 Q151 T2
>
> I have cobbled together some code which imports the data. The next
> step is to create an array in which each column contains a years worth
> of values. Thus, if i have 6 years of data (2001-2006 inclusive),
> there will be six columns, with 365 rows (not all years have a full
> data set and may only have say 340 days of data.
> <The question>
> In the code below
> print answer[j,1] is giving me the right answer but i can't write it
> to an array.
> any suggestions welcomed.
>
> This is what I have:
> flow=[]
> flowdate=[]
> yeardate=[]
> uniqueyear=[]
> #flow_order=
> flow_rank=[]
> icount=[]
> p=[]
>
> filename=r"C:\Documents and Settings\bevanj\Desktop\flow_duration.tsf"
> linesep ="\n"
>
> # read in whole file
> tempdata = open( filename).read()
> # break into lines
> tempdata = string.split( tempdata, linesep )
> # for each record, get the field values
> for i in range( len( tempdata)):
> # split into the lines
> fields = string.split( tempdata[i])
> if len(fields)>5:
> flowdate.append(fields[0])
> list =string.split(fields[0],"/")
> yeardate.append(list[2])
> flow.append(float(fields[3]))
> answer=column_stack((flowdate,flow))
>
> for rows in yeardate:
> if rows not in uniqueyear:
> uniqueyear.append(rows)
>
> #print answer[:,0] #date
> flow_order=empty((0,0),dtype=float)
> #for yr in enumerate(uniqueyear):
> for iyr,yr in enumerate(uniqueyear):
> for j, val, in enumerate (answer[:,0]):
> flowyr=string.split(val,"/")
> if int(flowyr[2])==int(yr):
> print answer[j,1]
> #flow_order =
Maybe you're looking for something more in the line of:
fInput = open('tst.txt')
dictObj = {}
"""{ Year_Key: { DayKey: FloatValue}}"""
for each_line in fInput.readlines():
if each_line.strip():
line = each_line.strip().split()
if len(line) == 6:
if dictObj.has_key(line[0].split('/')[-1]):
tmpDict = dictObj[line[0].split('/')[-1]]
tmpDict[line[0]] = line[3]
else:
dictObj[line[0].split('/')[-1]] = {line[0]:line[3]}
fInput.close()
More information about the Python-list
mailing list