I am working on a script that involves incidents of fire. I'm taking my values from a text file that is comma delimited. The first line of the text file contains the field names (Latitude,Longitude,Confidence). My goal is to create a new feature class that will take the last field name from my text file (e.g. Confidence) and use this as the field name in my new feature class. I have succeeded in getting arcgis to read the last field name from my text file so that a new feature class is created with the field names: "ID, SHAPE, Confidence", however, my feature class is empty- my for loops seems to stop after reading the first line. I believe that I have the right components, I'm just not sure what order to put them in. The part that confuses me the most is, I start reading the text file in a for loop (which I think is pretty normal) and my instinct is to split this text file and, using indexing, point to the last field name "Confidence" using a variable. Once I do this, I'm not sure how to make my insert cursor and Add Field tool (which are outside of the for loop) recognize this variable that points to "Confidence". Any suggestions?
Below is my code:
import arcpy work=r"C:\Users\Cara\Documents\WildlandFires.mdb" arcpy.env.workspace=work arcpy.env.overwriteOutput=True iFile= r"C:\Users\Cara\Documents\NorthAmericaWildfires_2007275.txt" with open(iFile, 'r') as f: lstFires = f.readlines() #new feature class name newFC= fireIncidents fc=newFC #user input mininum confidence threshold confidenceThreshold=99 minThresh=int(confidenceThreshold) lstFires = f.readlines() #Create New Feature Class arcpy.CreateFeatureclass_management(work, fc, "POINT", "", "", "", "", "", "", "", "") #Loop through file line by line and populate new rows in feature class #Create Insert Cursor to use for inserting new rows into feature class fields = ["SHAPE@", fieldName] cur = arcpy.da.InsertCursor (fc, fields) #add field arcpy.AddField_management(fc, fieldName , "SHORT") print "created fields" cntr = 0 #counter variable used to print out progress of script for fire in lstFires: if 'Latitude' in fire: #read line, use split function fn = fire.split(",") fieldName = fn continue lstValues = fire.split(",")#split out values into separate values if int(lstValues) > minThresh: # if confidence value is greater than user defined confidenc value, continue with code #create point object that will hold x,y coordinate of fire pnt = arcpy.CreateObject("Point") #pull out individual values using list indexing latitude = float(lstValues) longitude = float(lstValues) confid = int(lstValues) print "I can pull out values" cntr=cntr + 1 #assign x, y properties of Point object to long and lat values pnt.X = longitude pnt.Y = latitude #Create new row for feature class #assigns new Point object to "SHAPE" field and confidence value to "CONFIDENCEVALUE" field row = [pnt, confid] print "row's OK" #Insert new row to feature class cur.insertRow(row) print "Record # " + str(cntr) + " written to feature class" #release cursor lock on feature class del cur