Hi all,I executed the following script on the attached txt file:>>> # open the text file, read it into memory
... file = open('c:/testing/lat-lon-date-conversions.txt').read().splitlines()
... # define a new, empty list object
... l = []
... # iterate through the lines of the file, slice up the line by index
... # position of each data column
... for line in file:
... v1 = line[0:8].strip() # lat dms
... v2 = v1[0:2] # lat deg
... v3 = v1[2:4] # lat min
... v4 = v1[4:6]+'.'+v1[6:8] # lat sec
... v5 = str(int(v2)+(float(v3)/60)+(float(v4)/3600)) # lat dd
... v6 = line[9:23].strip() # lon dms
... v7 = v6[0:3] # lon deg
... v8 = v6[3:5] # lon min
... v9 = v6[5:7]+'.'+v6[7:9] # lon sec
... v10 = str('-'+str(int(v7)+(float(v8)/60)+float(v9)/3600)) # lon dd
... v11 = line[19:23] # date as MMYY
... v12 = str(v11[0:2])+'/'+str(v11[2:4]) # concatenate date as MM/YY
... # add our line to list l
... # do string formatting to lat/long to display only 5 decimal points
... l.extend([[('%.5f' % float(v5)),('%.5f' % float(v10)),v12]])
... # print out each line to stdout
... for each in l:
... print each
...
['36.44000', '-94.03667', '06/94']
['36.44000', '-94.04833', '06/94']
['33.09167', '-93.90333', '09/05']
['33.02667', '-93.89167', '07/05']
['34.34111', '-93.50056', '05/06']
['34.24833', '-93.67667', '01/07']
['34.22500', '-93.89500', '07/06']
['33.76833', '-92.48500', '07/06']
['33.74500', '-92.47667', '07/06']
['33.68000', '-92.46667', '07/06']
>>> import csv
... >>> # open and read the csv file into memory
... >>> file = open('C:/testing/simple-csv.csv')
... >>> reader = csv.reader(file)
... >>> # iterate through the lines and print them to stdout
... >>> # the csv module returns us a list of lists and we
... >>> # simply iterate through it
... >>> for line in reader:
... ... print line
... ...
...
Parsing error <type 'exceptions.SyntaxError'>: invalid syntax (line 2)
>>> import csv
>>> file = open('C:/testing/simple-csv.csv')
>>> reader = csv.reader(file)
>>> for line in reader:
... ... print line
... ...
...
Parsing error <type 'exceptions.IndentationError'>: expected an indented block (line 2)
>>> for line in reader:
... print line
...
...
['34.79038,-96.80871."4/13/1983"']
['34.93032,-96.44490,"2/5/1967"']
['34.95507,-96.92268,"12/23/2001"']
['34.95689,-96.92263,"8/9/1999"']
['34.92559,-96.68021,"8/25/1954"']
>>> import csv
>>> file = open('C:/testing/simple-csv.csv')
>>> reader = csv.reader(file)
>>> for line in reader:
... print line
...
['34.79038,-96.80871,"4/13/1983"']
['34.93032,-96.44490,"2/5/1967"']
['34.95507,-96.92268,"12/23/2001"']
['34.95689,-96.92263,"8/9/1999"']
['34.92559,-96.68021,"8/25/1954"']
>>> import arcgisscripting, csv, os
... gp = arcgisscripting.create()
... # Here we have three custom functions we will call later in our code
... def killObject( object ):
... """ Kills an input object """
... if gp.Exists(object):
... gp.Delete_management(object)
... def makeFgdb( dir, db, fd, spatRef ):
... """ Create a file geodatabase and featuredataset """
... if os.path.isdir(dir) != 1:
... os.mkdir(dir)
... killObject( os.path.join(dir, db) )
... gp.CreateFileGDB_management(dir, db)
... gp.CreateFeatureDataset_management(os.path.join(dir, db), fd, spatRef)
...
... def createGdbTable( db, table, fields ):
... """ Creates an empty standalone GDB table and adds fields provided in a list
... - with a set schema """
... killObject(db + '/' + table)
... gp.CreateTable(db, table)
... for field in fields:
... if field[1] == 'TEXT':
... gp.AddField_management(db + '/' + table,field[0],field[1],'#','#',field[2],field[3],'NULLABLE','NON_REQUIRED','#')
... else:
... gp.AddField_management(db + '/' + table,field[0],field[1],'#','#','#',field[3],'NULLABLE','NON_REQUIRED','#')
... # our spatial reference, this can be copied from a prj file
... sr = 'GEOGCS["GCS_North_American_1927",DATUM["D_North_American_1927",SPHEROID["Clarke_1866",6378206.4,294.9786982]],\
... PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]]'
... # our list of fields and their properties:
... # [field_name,field_type,field_length (zero for non-text fields),field_alias]
... fields = [['LAT','DOUBLE','0','Latitude'],
... ['LON','DOUBLE','0','Longitude'],
... ['SOMEDATE','TEXT','6','Some Date']]
...
... # create our new file geodatabase to work with
... makeFgdb( 'C:/Testing', 'NBI.gdb', 'Bridges', sr )
... # create our standalone geodatabase table and its schema
... createGdbTable( 'C:/Testing/NBI.gdb', 'Test', fields )
... # start slurping our text file into gdb table
... # create a cursor on the table
... rows = gp.InsertCursor('C:/Testing/NBI.gdb/Test')
... # open the text file, read it into memory
... file = open('c:/Testing/lat-lon-date-conversions.txt').read().splitlines()
... # define a new, empty list object
... l = []
... # iterate through the lines of the file, slice up the line by index
... # position of each data column
... for line in file:
... v1 = line[0:8].strip() # lat dms
... v2 = v1[0:2] # lat deg
... v3 = v1[2:4] # lat min
... v4 = v1[4:6]+'.'+v1[6:8] # lat sec
... v5 = str(int(v2)+(float(v3)/60)+(float(v4)/3600)) # lat dd
... v6 = line[9:18].strip() # lon dms
... v7 = v6[0:3] # lon deg
... v8 = v6[3:5] # lon min
... v9 = v6[5:7]+'.'+v6[7:9] # lon sec
... v10 = str('-'+str(int(v7)+(float(v8)/60)+float(v9)/3600)) # lon dd
... v11 = line[18:23] # date as MMYY
... v12 = str(v11[0:2])+'/'+str(v11[2:4]) # concatenate date as MM/YY
... # add our line to list l
... # do string formatting to lat/long to only display 5 decimal points
... l.extend([[('%.5f' % float(v5)),('%.5f' % float(v10)),v12]])
... ln = 0
... # iterate through our data
... for line in l:
... t = 0
... # create a new row
... row = rows.NewRow()
... # iterate through the fields
... for field in fields:
... val = line.strip()
... # set the value for each field
... if field[1] == 'DOUBLE':
... row.SetValue(field[0], '%.5f' % float(val))
... else:
... row.SetValue(field[0], val)
... t = t + 1
... # insert the row into the table
... rows.InsertRow(row)
... ln = ln + 1
... del row
... del t
... del line
... del rows
...
>>>
It worked. But the dates in the column "Some Date" are weird - see the attached mxd file.Please kindly help and advise me how I can fix it.Thanks,Scott Chang