Add date field to imported csv file

المشرف العام

Administrator
طاقم الإدارة
I am using a script written by John to import a csv file and make a point feature class. My table looks like this:

"timestamp_pretty","mmsi","imo","shipType","lat","long","month","week""31/12/1969 23:59:59",265650950,9544322,"HSC",57.661503,11.84175,12,1"31/12/1969 23:59:59",305706000,9583902,"CARGO",59.486267,20.078266,12,1As you see, there is a date field in my table. How to import it as date format instead of text?

# Author: John K. Tran# Contact: jtran20@masonlive.gmu.eduimport arcpyimport osimport csvimport timeimport localefrom arcpy import envstart_time = time.time()##incsv = arcpy.GetParameterAsText(0)##outfc = arcpy.GetParameterAsText(1)incsv = r"E:\DensityMaps\TestFilesLoop\test.csv" # Change this to the path of your CSV file.outfc = r"E:\DensityMaps\DensityMapsTest1.gdb\TestSampleFC" # Change this to the path of your output FC.spatialref = arcpy.SpatialReference(4326) # Create the spatial reference object as WGS84. Can modify if desired.if not arcpy.Exists(outfc): # Create the output feature class if needed. arcpy.CreateFeatureclass_management(os.path.dirname(outfc), os.path.basename(outfc), "POINT", None, None, None, spatialref)csv.register_dialect("xls", delimiter=",", lineterminator="\n") # Register the dialect for native CSV syntax in Microsoft Excel.f = open(incsv, "r")reader = csv.reader(f, dialect = "xls")headers = reader.next() # Read the first line as the header names.headerDate = headers.index("timestamp_pretty")for header in headers[0:]: # Add fields for remaining columns if needed. Default is TEXT field. arcpy.AddField_management(outfc, header , "TEXT") print "Header: "+headercursor = arcpy.da.InsertCursor(outfc, ['SHAPE@XY'] + headers[0:]) # Create InsertCursor.count = 0for row in reader: if count % 1000 == 0: print "processing row {0}".format(count) date = row[0] Ycoord = row[4] # Make sure 'Lat' is in the 4th column. Xcoord = row[5] # Make sure 'Lon' is in the 5th column. newrow = [(float(Xcoord), float(Ycoord))] + row[0:] cursor.insertRow(newrow) # Insert point in FC for each row in CSV. count += 1del cursorf.close()print ("--- %s seconds ---" % (time.time() - start_time))

أكثر...
 
أعلى