<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Working with csv and headers in Transportation Questions</title>
    <link>https://community.esri.com/t5/transportation-questions/working-with-csv-and-headers/m-p/137910#M446</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Original User: rexmorgan72&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Good day.&amp;nbsp; I have some code that I will post below.&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;import time import csv import array from time import gmtime, strftime print strftime("%Y-%m-%d %H:%M:%S", gmtime())&amp;nbsp;&amp;nbsp; print ' ' print '&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ARCGIS Climate Data Mapping Program' print ' '&amp;nbsp; # # This is an attempt to rectify the removal of headers in the csv file. ##&amp;nbsp;&amp;nbsp; fname = 'Z:\\Desktop\\COOP_rainfall2.csv'&amp;nbsp; # read the data file data_list = [] for line in open(fname): &amp;nbsp;&amp;nbsp;&amp;nbsp; line = line.rstrip() &amp;nbsp;&amp;nbsp;&amp;nbsp; # create a list &amp;nbsp;&amp;nbsp;&amp;nbsp; line_list = line.split(',') &amp;nbsp;&amp;nbsp;&amp;nbsp; data_list.append(line_list) print(line, data_list)&amp;nbsp; ### #### Printed the file out but it started with Zortman for some reason which fouled up the header issue.&amp;nbsp; time.sleep(10)&amp;nbsp;&amp;nbsp; # create a months dictionary with month:index pairs mdict = {} for ix, item in enumerate(data_list[0]): &amp;nbsp;&amp;nbsp;&amp;nbsp; print(ix, item)&amp;nbsp; # test prints out all headers and data positions &amp;nbsp;&amp;nbsp;&amp;nbsp; if ix &amp;gt; 8: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; mdict[item] = ix time.sleep(3) print('-'*70) #prints a row of dashes this helps seperate data&amp;nbsp; #Gathering input from user&amp;nbsp; 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&amp;nbsp; #assigning beginning and ending month here&amp;nbsp; month_start = month1 month_end = month2&amp;nbsp; #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&amp;nbsp;&amp;nbsp; for item in data_list[1:]: &amp;nbsp;&amp;nbsp;&amp;nbsp; #print(item) # test &amp;nbsp;&amp;nbsp;&amp;nbsp; station = item[0] &amp;nbsp;&amp;nbsp;&amp;nbsp; lat = item[2] #Remembe to change this value if altering the structure of the input file Z:\\Desktop\\COOP_rainfall2.csv &amp;nbsp;&amp;nbsp;&amp;nbsp; long = item[3] &amp;nbsp;&amp;nbsp;&amp;nbsp; start = mdict[month_start] &amp;nbsp;&amp;nbsp;&amp;nbsp; end = mdict[month_end]+1 &amp;nbsp;&amp;nbsp;&amp;nbsp; plist = [float(x) for x in item[start : end] if x] #having trouble with blanks &amp;nbsp;&amp;nbsp;&amp;nbsp; print(plist) # test prints out the values prior to adding them &amp;nbsp;&amp;nbsp;&amp;nbsp; if plist: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; mysum = sum(plist) &amp;nbsp;&amp;nbsp;&amp;nbsp; 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)&amp;nbsp; for item in new_list: &amp;nbsp;&amp;nbsp;&amp;nbsp; print(item) # this print statement seems to produce a different set of data than the print statement in line 65&amp;nbsp; #print('~'*70) # prints a row of 70 ~ signs &amp;nbsp;&amp;nbsp;&amp;nbsp; #outgroup.write("%s \n " % item) &amp;nbsp;&amp;nbsp;&amp;nbsp; outgroup.write(str(item)) #this does produce a file with the info contained therein but not quite what I want &amp;nbsp;&amp;nbsp;&amp;nbsp; #new_file.write("%s\n" % item)&amp;nbsp; #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: &amp;nbsp;&amp;nbsp;&amp;nbsp; f.write(','.join(map(str,row))+'\n')&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;One of the problems that I am having is getting the resultant file 'test.csv' to not strip off the headers.&amp;nbsp; Most of the code above has been pieced together from other bits.&amp;nbsp; 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.&amp;nbsp; 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.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The info contained in the csv file is Weather info. Station Name, Station Number, Lat, Long, Precip.&amp;nbsp; I want to plot the precip values by having the user input a couple of variables 'month1' and 'month2'.&amp;nbsp; I am going to attach the python file and the csv file.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sat, 16 Feb 2013 22:03:55 GMT</pubDate>
    <dc:creator>Anonymous User</dc:creator>
    <dc:date>2013-02-16T22:03:55Z</dc:date>
    <item>
      <title>Working with csv and headers</title>
      <link>https://community.esri.com/t5/transportation-questions/working-with-csv-and-headers/m-p/137910#M446</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Original User: rexmorgan72&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Good day.&amp;nbsp; I have some code that I will post below.&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;import time import csv import array from time import gmtime, strftime print strftime("%Y-%m-%d %H:%M:%S", gmtime())&amp;nbsp;&amp;nbsp; print ' ' print '&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ARCGIS Climate Data Mapping Program' print ' '&amp;nbsp; # # This is an attempt to rectify the removal of headers in the csv file. ##&amp;nbsp;&amp;nbsp; fname = 'Z:\\Desktop\\COOP_rainfall2.csv'&amp;nbsp; # read the data file data_list = [] for line in open(fname): &amp;nbsp;&amp;nbsp;&amp;nbsp; line = line.rstrip() &amp;nbsp;&amp;nbsp;&amp;nbsp; # create a list &amp;nbsp;&amp;nbsp;&amp;nbsp; line_list = line.split(',') &amp;nbsp;&amp;nbsp;&amp;nbsp; data_list.append(line_list) print(line, data_list)&amp;nbsp; ### #### Printed the file out but it started with Zortman for some reason which fouled up the header issue.&amp;nbsp; time.sleep(10)&amp;nbsp;&amp;nbsp; # create a months dictionary with month:index pairs mdict = {} for ix, item in enumerate(data_list[0]): &amp;nbsp;&amp;nbsp;&amp;nbsp; print(ix, item)&amp;nbsp; # test prints out all headers and data positions &amp;nbsp;&amp;nbsp;&amp;nbsp; if ix &amp;gt; 8: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; mdict[item] = ix time.sleep(3) print('-'*70) #prints a row of dashes this helps seperate data&amp;nbsp; #Gathering input from user&amp;nbsp; 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&amp;nbsp; #assigning beginning and ending month here&amp;nbsp; month_start = month1 month_end = month2&amp;nbsp; #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&amp;nbsp;&amp;nbsp; for item in data_list[1:]: &amp;nbsp;&amp;nbsp;&amp;nbsp; #print(item) # test &amp;nbsp;&amp;nbsp;&amp;nbsp; station = item[0] &amp;nbsp;&amp;nbsp;&amp;nbsp; lat = item[2] #Remembe to change this value if altering the structure of the input file Z:\\Desktop\\COOP_rainfall2.csv &amp;nbsp;&amp;nbsp;&amp;nbsp; long = item[3] &amp;nbsp;&amp;nbsp;&amp;nbsp; start = mdict[month_start] &amp;nbsp;&amp;nbsp;&amp;nbsp; end = mdict[month_end]+1 &amp;nbsp;&amp;nbsp;&amp;nbsp; plist = [float(x) for x in item[start : end] if x] #having trouble with blanks &amp;nbsp;&amp;nbsp;&amp;nbsp; print(plist) # test prints out the values prior to adding them &amp;nbsp;&amp;nbsp;&amp;nbsp; if plist: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; mysum = sum(plist) &amp;nbsp;&amp;nbsp;&amp;nbsp; 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)&amp;nbsp; for item in new_list: &amp;nbsp;&amp;nbsp;&amp;nbsp; print(item) # this print statement seems to produce a different set of data than the print statement in line 65&amp;nbsp; #print('~'*70) # prints a row of 70 ~ signs &amp;nbsp;&amp;nbsp;&amp;nbsp; #outgroup.write("%s \n " % item) &amp;nbsp;&amp;nbsp;&amp;nbsp; outgroup.write(str(item)) #this does produce a file with the info contained therein but not quite what I want &amp;nbsp;&amp;nbsp;&amp;nbsp; #new_file.write("%s\n" % item)&amp;nbsp; #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: &amp;nbsp;&amp;nbsp;&amp;nbsp; f.write(','.join(map(str,row))+'\n')&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;One of the problems that I am having is getting the resultant file 'test.csv' to not strip off the headers.&amp;nbsp; Most of the code above has been pieced together from other bits.&amp;nbsp; 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.&amp;nbsp; 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.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The info contained in the csv file is Weather info. Station Name, Station Number, Lat, Long, Precip.&amp;nbsp; I want to plot the precip values by having the user input a couple of variables 'month1' and 'month2'.&amp;nbsp; I am going to attach the python file and the csv file.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 16 Feb 2013 22:03:55 GMT</pubDate>
      <guid>https://community.esri.com/t5/transportation-questions/working-with-csv-and-headers/m-p/137910#M446</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2013-02-16T22:03:55Z</dc:date>
    </item>
    <item>
      <title>Re: Working with csv and headers</title>
      <link>https://community.esri.com/t5/transportation-questions/working-with-csv-and-headers/m-p/137911#M447</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;OK. I got this part solved. It is rather ugly and I am open to suggestions but this does work.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;#This code is working as of 2-17-2013 at 950 AM # sum some data of a csv file #This code now seems to be working as expected.&amp;nbsp;&amp;nbsp; import time import csv fname = 'Z:\\Desktop\\COOP_rainfall2.csv'&amp;nbsp; # read the data file data_list = [] for line in open(fname): &amp;nbsp;&amp;nbsp;&amp;nbsp; # remove trailing newline char &amp;nbsp;&amp;nbsp;&amp;nbsp; line = line.rstrip() &amp;nbsp;&amp;nbsp;&amp;nbsp; # create a list &amp;nbsp;&amp;nbsp;&amp;nbsp; line_list = line.split(',') &amp;nbsp;&amp;nbsp;&amp;nbsp; data_list.append(line_list)&amp;nbsp;&amp;nbsp; # create a months dictionary with month:index pairs mdict = {} for ix, item in enumerate(data_list[0]): &amp;nbsp;&amp;nbsp;&amp;nbsp; print(ix, item)&amp;nbsp; # test prints out all headers and data positions &amp;nbsp;&amp;nbsp;&amp;nbsp; if ix &amp;gt; 8: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; mdict[item] = ix time.sleep(3) print('-'*70) #prints a row of dashes this helps seperate data&amp;nbsp; #Gathering input from user&amp;nbsp; 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&amp;nbsp; #assigning beginning and ending month here&amp;nbsp; month_start = month1 month_end = month2&amp;nbsp; #Create a new list new_list = [] outgroup = file("Z:\\Desktop\\test.csv", 'w') #this file is initially in the wrong format. for item in data_list[1:]: &amp;nbsp;&amp;nbsp;&amp;nbsp; #print(item) # test &amp;nbsp;&amp;nbsp;&amp;nbsp; station = item[0] &amp;nbsp;&amp;nbsp;&amp;nbsp; lat = item[2] #Remembe to change this value if altering the structure of the input file Z:\\Desktop\\COOP_rainfall2.csv &amp;nbsp;&amp;nbsp;&amp;nbsp; long = item[3] &amp;nbsp;&amp;nbsp;&amp;nbsp; start = mdict[month_start] &amp;nbsp;&amp;nbsp;&amp;nbsp; end = mdict[month_end]+1 &amp;nbsp;&amp;nbsp;&amp;nbsp; plist = [float(x) for x in item[start : end] if x] #having trouble with blanks &amp;nbsp;&amp;nbsp;&amp;nbsp; print(plist) # test prints out the values prior to adding them &amp;nbsp;&amp;nbsp;&amp;nbsp; if plist: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; mysum = sum(plist) &amp;nbsp;&amp;nbsp;&amp;nbsp; new_list.append([station, lat, long, mysum]) print('-'*70) #prints another row of dashes further seperating data print("Result:")&amp;nbsp; #### Adding these next two lines for a test #print([new_list]) #added the brackets to see if this makes a difference 2-17-2013 908 AM #print('~'*80)&amp;nbsp; for item in new_list: &amp;nbsp;&amp;nbsp;&amp;nbsp; #print(item) &amp;nbsp;&amp;nbsp;&amp;nbsp; #outgroup.write("%s \n " % item) &amp;nbsp;&amp;nbsp;&amp;nbsp; outgroup.write(str(item)) #this does produce a file with the info contained therein but not quite what I want &amp;nbsp;&amp;nbsp;&amp;nbsp; #new_file.write("%s\n" % item) f= open("Z:\\Desktop\\test2.csv", 'w') # This file needs to be different than the one in line 35 of code.&amp;nbsp; #f= open("test2.csv", 'w') for row in new_list: &amp;nbsp;&amp;nbsp;&amp;nbsp; f.write(','.join(map(str,row))+'\n')&amp;nbsp;&amp;nbsp; #Another test&amp;nbsp; #### new_list.insert(0,['Station', 'lat', 'long', 'mysum']) # this ran without errors and did add the values to the beginning of list ## this line added at 2-17-2013 933 AM&amp;nbsp; ##### Adding these same lines of code found in lines&amp;nbsp; 60, 62 and 63 f= open("Z:\\Desktop\\test3.csv", 'w') for row in new_list: &amp;nbsp;&amp;nbsp;&amp;nbsp; f.write(','.join(map(str,row))+'\n') #The above code was added 2-17-2013 939 AM this is a test #I am not sure what is going on now. Before I would get 3 differenct csv files which is what i was expecting # now I only get this one. This is is correct but not sure what to do about it.&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # the line below is producing an error&amp;nbsp; #new_list.append(('Station', 'lat', 'long', 'mysum')) #this is a new line for testing 2-17-2013 911 AM #this does seems to append the Station, lat, long, mysum but it puts it at the end not the beginning.&amp;nbsp; #print([new_list]) #added the brackets to see if this makes a difference 2-17-2013 914 AM #print('~'*80)&amp;nbsp; time.sleep(15) &lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Like I said, I know it's ugly. Now I seems to be having another set of issues.&amp;nbsp; I got a decent output file. I will attach it to this post. The problem now is getting ArcGIS to recognize the new csv file in the arcpy.MakeXYEventLayer_management.&amp;nbsp; I have the file that I attached test3.csv I am hoping to read that data into ArcGIS and plot accordingly.&amp;nbsp; Any suggestions on this part would be greatly appreciated. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;The error is in line 151 arcpy.MakeXYEventLayer_management(Rex, "Long", "Lat", Var_layer, spRef, "")&lt;BR /&gt;ERROR 000728: Field Long does not exist within table&lt;BR /&gt;ERROR 000728: Field Lat does not exist within table&lt;BR /&gt;Failed to execute (MakeXYEventLayer).&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The code follows in a separate post : Sorry about the long post.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 17 Feb 2013 16:51:10 GMT</pubDate>
      <guid>https://community.esri.com/t5/transportation-questions/working-with-csv-and-headers/m-p/137911#M447</guid>
      <dc:creator>RexMorgan</dc:creator>
      <dc:date>2013-02-17T16:51:10Z</dc:date>
    </item>
  </channel>
</rss>

