Good Day, I have a text file in the .csv format. The file originates as a xlsx (excel) file type. The file looks something like the following.
Name Number Lat Long Jan Feb Mar Apr
Brockton 24-1670-06 38.145236 145.854921 0.15 0.62 1.25 2.14
Westby 24-7432-04 35.846125 132.743652 0.25 0.94 1.14 2.01
Zortman 24-9910-06 38.418461 125.541525 0.31 0.75 1.51 1.84
Now what I would like to do is select a starting month and an ending month and add them together. For example using the above info, selecting a starting month of Feb and an ending month of Apr add the data and create something like the following.
Name Number Lat Long Sum
Brockton 24-1670-06 38.145236 145.854921 4.01
Westby 24-7432-04 35.846125 132.743652 4.09
Zortman 24-9910-06 38.418461 125.541525 4.10
What I have so far is the following code. The problem that I have is that I am not sure how to write the results to a new csv or xlsx (excel) for further use in the remainding code.
#This code is working as of 9-7-2012 at 550AM
# sum some data of a csv file
import time
import csv
fname = 'Z:\\Desktop\\COOP_rainfall.csv'
# read the data file
data_list = []
for line in open(fname):
# remove trailing newline char
line = line.rstrip()
# create a list
line_list = line.split(',')
data_list.append(line_list)
# create a months dictionary with month:index pairs
mdict = {}
for ix, item in enumerate(data_list[0]):
print(ix, item) # test prints out all headers and data positions
if ix > 8:
mdict[item] = ix
time.sleep(3)
print('-'*70) #prints a row of dashes this helps seperate data
month1=raw_input('Input the 3 letter ID of beginning month')#getting input data from user
month2=raw_input('Input the 3 letter ID of ending month')#getting input data from user
#assigning beginning and ending month here
month_start = month1
month_end = month2
#Create a new list
new_list = []
for item in data_list[1:]:
#print(item) # test
station = item[0]
lat = item[1]
long = item[2]
start = mdict[month_start]
end = mdict[month_end]+1
plist = [float(x) for x in item[start : end] if x] #having trouble with blanks
print(plist) # test prints out the values prior to adding them
if plist:
mysum = sum(plist)
new_list.append([station, lat, long, mysum])
print('-'*70) #prints another row of dashes further seperating data
print("Result:")
for item in new_list:
print(item)
time.sleep(45)
I am still kinda new to python. Also there is a restriction of not using anything beyond python 2.7. The reason for this is that the code above is a small snippett from a larger work. The idea is that I will export my precipitation data from the excel file into a csv file read in the file add up the appropriate months and send that info to ArcGIS 10.1 to plot the data on a map that is appropriate. Thanks in advance for any help.
Rex