Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I have a folder with multiple text files. I am writing a code that reads all of the text files, and creates a point feature class (i.e. a shape file) for each one. The text files are all tab delineated with the same set up "Id, name, type, lat, long" I have posted the code I have below. A shape file is created for each text file, and the fields are created for the attribute table, however, the points are not created, and the rows are not being inserted into the attribute table. I had it working for an individual text file but I would like to do it for multiple text files. Any help would be appreciated.

importing arcpy and os

import arcpy, os from arcpy import env

arcpy.env.overwriteOutput = True

Location = r"C:\temp"

Set environment settings

env.workspace = Location

for file in os.listdir(Location):

if file.endswith(".txt"):
    try:

        fcName = os.path.splitext(file)    
        outName = fcName[0]  + '.shp'  

        outFolder = r'c:/temp/Data'


            #Creating the feature class
        datasetCreate = arcpy.CreateFeatureclass_management(outFolder, outName, "Point", "", "", "")

        cursor = arcpy.InsertCursor(datasetCreate)



            #Creating the fields for the attribute table
        arcpy.AddField_management(datasetCreate, "Name", "TEXT", "", "", 40)
        arcpy.AddField_management(datasetCreate, "Type", "TEXT", "", "", 20)
        arcpy.AddField_management(datasetCreate, "Lat", "DOUBLE", "", "", 20)
        arcpy.AddField_management(datasetCreate, "Long", "DOUBLE", "", "", 20)



                #Open the text file and read it 
        textfile = open(file, "r")

                    #Create a variable for the headerline
        headerline = textfile.readline()


                    #Get rid of the headerline, and split the text file into columns
        valueList = headerline.strip().split("\t")
        print valueList
        Lat = valueList.index("Lat")
        Long = valueList.index("Long")
        Name = valueList.index("Name")
        Type = valueList.index("Type")
        ID = valueList.index("ID")

                    #Insert a cursor using arcpy  



        for point in textfile.readlines():
            segmentedPoint = point.split("\t")
            latvalue = segmentedPoint[Lat]
            longvalue = segmentedPoint[Long]
            namevalue = segmentedPoint[Name]
            typevalue = segmentedPoint[Type]
            idvalue = segmentedPoint[ID]

                    #Create a point object and set the lat and long fields to the x and y coordinates    
            vertex = arcpy.CreateObject("Point")
            vertex.X = latvalue
            vertex.Y = longvalue



                    #Use the cursor to insert each new row for the attribute table  
            feature = cursor.newRow()      
            feature.shape = vertex
            feature.setValue("ID", idvalue)
            feature.setValue("Name", namevalue)
            feature.setValue("Type", typevalue)
            feature.setValue("Lat", latvalue)
            feature.setValue("Long", longvalue)

            cursor.insertRow(feature)

                        # If an error occurred print the message to the screen

    except:
            # If an error occurred print the message to the screen
        print arcpy.GetMessages()            

                    #delete the cursor
        del cursor
share|improve this question
    
try with the "del cursor" in the "try:" loop and not in the "except:" loop –  radouxju Feb 17 '14 at 7:01
    
also it would be much easier with arcpy.da.insertcursor instead of arcpy.insertcursor –  radouxju Feb 17 '14 at 7:02

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.