import arcgisscripting, csv gp=arcgisscripting.create(9.3) output=open(r'C:\GIS Projects\sandbox\test.csv','w') linewriter=csv.writer(output,delimiter=',') fcdescribe=gp.Describe('C:\\GIS Projects\\sandbox\\New_Shapefile(3).shp') flds=fcdescribe.Fields header=[] for fld in flds: value=fld.Name header.append(value) linewriter.writerow(header) cursor=gp.SearchCursor('C:\\GIS Projects\\sandbox\\New_Shapefile(3).shp') row=cursor.Next() while row: line=[] for fld in flds: value=row.GetValue(fld.Name) line.append(value) linewriter.writerow(line) del line row=cursor.Next() del cursor output.close()
header=['Id','C','D','AtoC','BtoD'] linewriter.writerow(header) cursor=gp.SearchCursor('C:\\GIS Projects\\sandbox\\New_Shapefile(3).shp') row=cursor.Next() while row: line=[] for fld in header: value=row.GetValue(fld) line.append(value) linewriter.writerow(line) del line row=cursor.Next()
import arcgisscripting, os gp = arcgisscripting.create() gp.workspace = os.getcwd() for table in os.listdir(''): if table.endswith('dbf'): # Make the output file name the same as the shapfile outFile = open(table[:-3]+"csv", 'a') ## Append all the field names to the fldList fldList = [] fld = gp.listFields(table) field = fld.next() while field: # Don't include ESRI's "FID" or "SHAPE" or "OID" fields. if field.name <> "FID" and field.name <> "Shape" and field.name <> "OID": fldList.append(field.name) field = fld.next() ## Write the field names to the outFile # Get the length of the fldList so we know when not to add the delimiter total = len(fldList) count = 0 for fieldName in fldList: count = count+1 delimeter = "," # If you're not the last value in the list, add the delimiter; else, don't add it if not count == total: outFile.write(fieldName+delimeter) else: outFile.write(fieldName+"\n") printrow = 0 ## Write the row values to the outFile rows = gp.SearchCursor(table) row = rows.Next() while row: printrow=printrow+1 print printrow # Create a list for each row valList = [] for fieldName in fldList: delimeter = "," value = row.GetValue(fieldName) # Append each row of values to the valList valList.append(value) count = 0 # Go through the list of values and add the delimiter for val in valList: count = count+1 # If you're not the last value in the list, add the delimiter; else, don't add it if not count == total: outFile.write(str(val)+delimeter) else: outFile.write(str(val)+"\n") row = rows.Next() print "\nDone.\n"
>>> no_good_fields ['FID', 'Shape', 'OID'] >>> all_fields ['Area', 'Perimeter', 'Shape', 'FID', 'OID'] >>> good_fields = list(set(all_fields) - set(no_good_fields)) >>> for a_field in good_fields: ... print a_field ... Perimeter Area >>> no_good_fields = ['FID', 'Shape', 'OID'] >>> all_fields = ['Area', 'Perimeter', 'Shape', 'FID', 'OID'] >>> for a_field in all_fields: ... if a_field not in no_good_fields: ... print a_field ... Area Perimeter
import arcgisscripting, csv gp=arcgisscripting.create(9.3) output=open(r'C:\GIS Projects\sandbox\test.csv','w') linewriter=csv.writer(output,delimiter=',') fcdescribe=gp.Describe('C:\\GIS Projects\\sandbox\\New_Shapefile(3).shp') flds=fcdescribe.Fields header=[] for fld in flds: value=fld.Name header.append(value) linewriter.writerow(header) cursor=gp.SearchCursor('C:\\GIS Projects\\sandbox\\New_Shapefile(3).shp') row=cursor.Next() while row: line=[] for fld in flds: value=row.GetValue(fld.Name) line.append(value) linewriter.writerow(line) del line row=cursor.Next() del cursor output.close()
# Import system modules import sys, string, os, arcgisscripting, csv # Create the Geoprocessor object gp = arcgisscripting.create() # Local variables... OutputFileName = sys.argv[1] Target_FC = sys.argv[2] output=open(OutputFileName,'w') linewriter=csv.writer(output,delimiter=',') fcdescribe=gp.Describe(Target_FC) flds=fcdescribe.Fields header=[] for fld in flds: value=fld.Name header.append(value) linewriter.writerow(header) cursor=gp.SearchCursor(Target_FC) row=cursor.Next() while row: line=[] for fld in flds: value=row.GetValue(fld.Name) line.append(value) linewriter.writerow(line) del line row=cursor.Next() del cursor output.close()
try: #9.2 and below fields.Reset() field = fields.Next() while field: if field.Type in ok_fields: num_fields.append(field.Name) field = fields.Next() except: #9.3 and above for field in fields: if field.Type in ok_fields: num_fields.append(field.Name)
output=open(r'C:\GIS Projects\sandbox\test.csv','wb')
outfile=open(YOUR CSV.csv'w') #Open a new file to write FeatureClass=arcpy.Describe(YOUR FEATURE CLASS) fields=FeatureClass.Fields #Use arcpy.Describe to get the field names from the export of parce_test header=[] #An empty list that will be populated with the field names for field in fields: #For a field in the list fields, header.append(field.name) #append the name to the header list outfile.write('%s\n' % ','.join(header[2:]))#Write the header list to the TXT. The list the sliced using [2:] to drop the first two list items which are OID and Shape because I dont want them cursor=arcpy.SearchCursor(YOUR FEATURE CLASS) for row in cursor try: line=[]#Empty list to hold the data for the each row for field in fields:#Same deal as before. For a field in the list fields, line.append(row.GetValue(field.Name))#add that to the end of the list "line" and repeat till out of fields outfile.write('%s\n' % ','.join(line[2:]))#Write the list to the TXT, again dropping the first two fields except: print 'Borked row: %s' % row.OID #If a line has a problem print the OID and move on. del cursor #Delete the cursor object because this is what is holding a schema lock outfile.close()#Close the TXT so that a lock is dropped from it as well.
outfile=open(YOUR CSV.csv'w') #Open a new file to write header=["ID", "x_1", "y_2", "i", "r", "height"] outfile.write('%s\n' % ','.join(header))#Write the header list to the TXT. cursor=arcpy.SearchCursor(YOUR LIDAR) for row in cursor try: line=[] for field in header: line.append(row.GetValue(field)) outfile.write('%s\n' % ','.join(line)) except: print 'Borked row: %s' % row.OID del cursor outfile.close()