AnsweredAssumed Answered

Use txt file as Fields in attribute table

Question asked by Aleah2vw on Feb 5, 2017
Latest reply on Feb 6, 2017 by Aleah2vw

I am attempting to write a script so that instead of hardcoding the name of the new field as "CONFIVALUE" for the confidence value, I want to use the name of the third column in the header of the input text file.


For example, let's say that the first line of the input text file looks like this:

Latitude,Longitude,Confidence

In this case the field name should be "Confidence".

 

If the first line looked like this:

Latitude,Longitude,ConfidValue

Then the field name should be "ConfidValue".

 

The error if i have conField indexed as conField[2]:

Traceback (most recent call last):
File "C:\Users\Aleah\Desktop\PythonScripts\Lab7_Q5.py", line 22, in <module>
IndexError: list index out of range


The error i am receiving if i remove the index is:

Traceback (most recent call last):
File "C:\Program Files (x86)\Wing IDE 101 4.1\src\debug\tserver\_sandbox.py", line 27, in <module>
File "C:\Program Files (x86)\ArcGIS\Desktop10.3\arcpy\arcpy\management.py", line 3246, in AddField
raise e
RuntimeError: Object: Error in executing tool

 

 

    import arcpy

    work = "C:\Scripts\Lab 7 Data\WildlandFires.mdb"
     arcpy.env.workspace = work # Set the workspace to the geodatabase
     arcpy.env.overwriteOutput = True

     iFile = "C:\Scripts\Lab 7 Data\NorthAmericaWildfires_2007275.txt"

     output = "NewFires.shp"
     threshold = 99

     f = open(iFile, 'r')# input text file C:\Scripts\Lab 7 Data\NorthAmericaWildfires_2007275.txt in "read" mode

     lstFires = f.readlines() # Read the lines of fire
     conField = []

     for line in f:
         comma = line.split(',')
         conField = comma
         print conField[2]

 

     field = ["SHAPE@", conField[2]] # represents the field names for each row in the feature class
     # Process: Create Feature Class

     arcpy.CreateFeatureclass_management(work, output, "POINT")

     arcpy.AddField_management(output, conField[2], "FLOAT")

     cursor_new = arcpy.da.InsertCursor(output, field)# add the new points that you will create to the "NewFires" feature class


     f.close() # close the file
     del cursor_new # release the cursor lock on the feature class

 

Outcomes