On 5/14/2012 10:16 PM, questions anon wrote:
I am completely new to dictionaries and I am not even sure if this is what I need to use. I have a text file that I would like to run summary stats on particular months, years and climate indices (in this case the climate indices are rainfall and fire area, so not actualy climate indices at all).
I would set up a SQLite database with a table of 4 numeric columns: year, month, rainfall, firearea Use SQL to select the desired date range and do the max and avg calculations: select year, avg(firearea), max(rainfall) from table where year = 1973 and month between 6 and 8)

you can use dictionaries but that will be harder. Here a start (untested). Assumes data are correct.

months = dict(Jan=1,Feb=2,Mar=4,Apr=4,May=5,Jun=6,Jul=7,Aug=8,Sep=9.Oct=10,Nov=11,Dec=12)
for line in open('d:/yearmonthrainfire.txt','r'):
    line = line.split()
    year = int(line[0])
    month = months[line[1]]
    rainfall = float(line[2]
    firearea = float(line[3]
sql = "insert into table (year, month, rainfall, firearea) values(%i,%i,%f,%f)" % (year, month, rainfall, firearea)
    # I don't have handy how one runs the sql

A text file is attached but a small sample of the file:
                  rainfall    firearea
1972 Jan    12.7083199    0
1972 Feb    14.17007142    0
1972 Mar    14.5659302    0
1972 Apr    1.508517302    0
1972 May    2.780009889    0
1972 Jun    1.609619287    0
1972 Jul    0.138150181    28
1972 Aug    0.214346148    32
1972 Sep    1.322102228    34747.8
1972 Oct    0.092663137    3655.9
1972 Nov    1.852276635    85.1
1972 Dec    2.011206002    42959.6
1973 Jan    5.55704346    153.5
1973 Feb    12.60326356    116.2
1973 Mar    11.08849105    223.6
1973 Apr    5.864925449    2.4
......

I have used an example from a book (a primer on scientific programming with python) and it seems to be working (see below) but I am not sure if I have my keys etc. are set up correctly to then begin anlaysis, and even how to use the dictionaries in my analysis . For example how can I print out the year with calculated the mean 'firearea' of June-July-August for that year along with the maximum 'rainfall' for June-July-august of the same year?
Any feedback will be greatly appreaciated!

infile=open('d:/yearmonthrainfire.txt','r')
lines=infile.readlines()
infile.close()
data={} #data[index][year]=indexvalue
first_line=lines[0]
climateindexname=first_line.split()
for index in climateindexname:
    data[index]={}
YEAR={}
MONTH={}

for line in lines[2:]:
    words=line.split()
    year=words[0] #years
    YEAR[year]={}
    month=words[1] #months
    MONTH[month]={}
    values=words[2:] #values of climateindices
    for index, v in zip(climateindexname, values):
        if v !=' ':
            data[index][year]=float(v)

print "years=", YEAR
print "months=", MONTH
print "data=", data
We usually reserve all caps names for constants.
You have way too many dictionaries.
Your program seems very complex for a very simple task.
I will not attempt to figure out what it does.




_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


--
Bob Gailer
919-636-4239
Chapel Hill NC

_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to