Good day. I have some code that I will post below.
One of the problems that I am having is getting the resultant file 'test.csv' to not strip off the headers. Most of the code above has been pieced together from other bits. Ultimately what I am trying to do is import a csv file add a few fields together and feed that data to ArcGIS 10.1. I have everything working except for the part where I need to feed it to ArcGIS. For some reason (probably a very simple one) I cannot seems to get the file to write without removing the header row.
The info contained in the csv file is Weather info. Station Name, Station Number, Lat, Long, Precip. I want to plot the precip values by having the user input a couple of variables 'month1' and 'month2'. I am going to attach the python file and the csv file.
Code:
import time
import csv
import array
from time import gmtime, strftime
print strftime("%Y-%m-%d %H:%M:%S", gmtime())
print ' '
print ' ARCGIS Climate Data Mapping Program'
print ' '
#
# This is an attempt to rectify the removal of headers in the csv file.
##
fname = 'Z:\\Desktop\\COOP_rainfall2.csv'
# read the data file
data_list = []
for line in open(fname):
line = line.rstrip()
# create a list
line_list = line.split(',')
data_list.append(line_list)
print(line, data_list)
###
#### Printed the file out but it started with Zortman for some reason which fouled up the header issue.
time.sleep(10)
# 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
#Gathering input from user
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 = []
outgroup = file("test.csv", 'w') #this file is getting put in the Rex.Morgan(\\204.228.188.2\HOME) (Z:) location
for item in data_list[1:]:
#print(item) # test
station = item[0]
lat = item[2] #Remembe to change this value if altering the structure of the input file Z:\\Desktop\\COOP_rainfall2.csv
long = item[3]
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(item, new_list) #this still prints the data with the Zortman line first
print('~'*70)
print(item) # this only prints the Zortman row of data; I am very confused here.
print('^'*70)
for item in new_list:
print(item) # this print statement seems to produce a different set of data than the print statement in line 65
#print('~'*70) # prints a row of 70 ~ signs
#outgroup.write("%s \n " % item)
outgroup.write(str(item)) #this does produce a file with the info contained therein but not quite what I want
#new_file.write("%s\n" % item)
#print (item)
f= open("test.csv", 'w') # This file is ending up in the Rex.Morgan(\\204.228.188.2\HOME) (Z:) location
#f= open("test2.csv", 'w')
for row in new_list:
f.write(','.join(map(str,row))+'\n')
The info contained in the csv file is Weather info. Station Name, Station Number, Lat, Long, Precip. I want to plot the precip values by having the user input a couple of variables 'month1' and 'month2'. I am going to attach the python file and the csv file.