[Arcmap10]python script to add and populate a field for several shapefiles

790
9
10-18-2011 06:07 AM
yoanndrocourt
New Contributor
Hi every one,
I'm realy new to python scripting, and I'm trying to write a script allowing me to create a shp file from a txt file, then add a new field to it and then fill this new field with a value extracted from the name of the file...this for several hundreds of txt files...
I have put together several bit of script but I can't get it to work...for some reason it stopped at the stage of creating the feature "arcpy.CreateFeaturesFromTextFile_samples"
could any one help me, that would be much appriciated

Ps ; the script in attached to this post

best regards
Yoann
Tags (2)
0 Kudos
9 Replies
ChristopherStorer
New Contributor
What sort of error did you receive when running the script?  This will help pinpoint the problem.
0 Kudos
yoanndrocourt
New Contributor
thank really for your help 😉
it's the same error than in my other message...it has to do with the toolbox I think..
cause it print the date extracted from the file, the txt name and shp name,
and then crashes
but I don't really know what else I can do..

22/06/2008
ASA_APM_1PNPDK20080622_131859_000000442069_00382_33001_1771_ok.shp
ASA_APM_1PNPDK20080622_131859_000000442069_00382_33001_1771_ok.txt
got to here
An unknown error occurred:
F:\front\create_feature_from_txt_batch_script3.py:63: DeprecationWarning: BaseException.message has been deprecated as of Python 2.6
Object: Tool or environment <????????????????????> not found
0 Kudos
RDHarles
Occasional Contributor
Give this version of your script a try. 
The tool "CreateFeaturesFromTextFile_samples" has been depreciated meaning that the "functionality has been replaced by other tools".
But the tool is still there in Arc10, it's just hidden so you can still access it from python through gp.

I didn't test the script, but you should be able to at least access that tool now.


#------------------------------------------------------------------------------------------------
#------------------------- script to create features from txt file ------------------------
#------------------------------------------------------------------------------------------------

# Create geoprocessing dispatch object
import arcgisscripting, glob
gp = arcgisscripting.create() # allow you to access the geoprocessing tools in arcgis

#------------------------------------------------------------------------------------------------
try:
    
    outdir = "F:\\front\\trash"
    filePaths = glob.glob("F:\\front\\trash\\*_ok.txt")
    print filePaths

    for filePath in filePaths:

        inTxt = filePath[15:]
        inSep = "local decimal point" 
        shpFile = filePath[15:-7] + "_ok.shp" #outdir +
        shpFile_Ok = shpFile
        fieldVal = shpFile[20:22] + "/" + shpFile[18:20] + "/" + shpFile[14:18]
        print fieldVal
        print shpFile
        print inTxt
        print 'got to here'
        
        # Run tool
        gp.CreateFeaturesFromTextFile_samples(inTxt, inSep , shpFile, \
        "PROJCS['Polar Stereographic', GEOGCS['GCS_WGS_1984',DATUM['D_WGS_1984', \
        SPHEROID['WGS_1984',6378137.0,298.257223563]], PRIMEM['Greenwich',0.0], \
        UNIT['Degree',0.0174532925199433]],PROJECTION['Stereographic_North_Pole'], \
        PARAMETER['False_Easting',0.0],PARAMETER['False_Northing',0.0],PARAMETER['Central_Meridian',-45.0], \
        PARAMETER['Standard_Parallel_1',71.0], UNIT['Meter',1.0]] \
        ;-30636100 -30636100 147003033.263715;-100000 10000;-100000 10000;0.001;0.001;0.001; IsHighPrecision")
        print 'Got to there...'

        # Process: Add Field
        gp.AddField_management(shpFile, "date", "DATE", "", "", "", "", "NON_NULLABLE", "NON_REQUIRED", "")        
        
        # Process: Calculate Field
        gp.CalculateField_management(shpFile_ok, "date", "fieldVal", "PYTHON_9.3", "")

except:
    print gp.GetMessages() # allows to get error message when they happen
0 Kudos
yoanndrocourt
New Contributor
Hello,
thank you so much for your reply, yes it is working kind of better...now the problem is when it has to write the value "fieldVal" in the field...when I open the resulting dbf file...the value is not the one expected...and it only work for the first file of the directory and then crashes...
but thanks for this reply...it helped me a lot already
regards
Yoann
0 Kudos
RDHarles
Occasional Contributor
What does the error message look like?
0 Kudos
yoanndrocourt
New Contributor
Hi,
It finally worked...the only thing is that the date written in the file is not the one expected..
it is writting the same numer in all files in the form 18991230...
don't really know what is hapening...but it worked well when I used the function to automatically print the actual date...
0 Kudos
yoanndrocourt
New Contributor
Hi all,
I am still strugling to write in the newly created field a date extracted from the name of a file..
I did manage to extract corectly the date in the form yyyy-mm-dd, however when I opens the attribute table in arcmap, a different date appears (for example it should be 22/06/2008 but instead I get 02/06/1905)...
each new shapefile have a different date, but not the one expected...
any idea, why would that happens???
any help would be very much appreciated...
thanks
0 Kudos
RuthEmerick
New Contributor II
Remove the quotes around fieldVal in the CalculateField_management line. It's a variable, not a string (yes, the variable CONTAINS a string, but in order for you to get the value of the variable, it must read it as a variable and not as the character positions of the string input). Like this:

# Process: Calculate Field
gp.CalculateField_management(shpFile_ok, "date", fieldVal, "PYTHON_9.3", "")
0 Kudos
yoanndrocourt
New Contributor
I got it working,
but thanks for your help anyway,
cheers
0 Kudos