Select to view content in your preferred language

Use txt file as Fields in attribute table

4217
12
Jump to solution
02-05-2017 11:59 AM
AleahWorthem
Deactivated User

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

0 Kudos
12 Replies
DarrenWiens2
MVP Alum

So, now that you've got rid of the new line character using strip(), you can access it similar to how you were doing it before, using the [2] index:

lstFires[0].strip().split(',')[2]
RandyBurton
MVP Alum

Your code:

output = "NewFires.shp"
# ....
arcpy.CreateFeatureclass_management(work, output, "POINT")‍‍‍‍‍‍‍‍‍‍‍

I get an error 000354 : The name contains invalid characters (a table in a file geodatabase cannot include a space or a period).

But, you are using an Access database?

work = "C:\Scripts\Lab 7 Data\WildlandFires.mdb"
0 Kudos
AleahWorthem
Deactivated User

Syntax

CreateFeatureclass_management (out_path, out_name, {geometry_type}, {template}, {has_m}, {has_z}, {spatial_reference}, {config_keyword}, {spatial_grid_1}, {spatial_grid_2}, {spatial_grid_3})
the MDB seems to be working
0 Kudos