POST
|
I think the only problem is you need to add the '.csv' extension to your test file (otherwise it does not know what file type to open). This will only work if you have ArcGIS 10.1 though since it uses the data access module. I should also add that in the above script you created the function, but you did not run it. The line that actually will run this is highlighted in blue. The input variables do not have to be named exactly like they are when you define the function either; Python only cares about what the variables are referencing.
import arcpy
from os import path as p
def TableToCSV(fc,CSVFile):
fields = [f.name for f in arcpy.ListFields(fc) if f.type <> 'Geometry']
with open(CSVFile, 'w') as f:
f.write(','.join(fields)+'\n') #csv headers
with arcpy.da.SearchCursor(fc, fields) as cursor:
for row in cursor:
f.write(','.join([str(r) for r in row])+'\n')
print 'Created %s Successfully' %p.basename(CSVFile)
if __name__ == '__main__':
fc = r"C:\Test\Test.gdb\Test1"
csv = r"C:\Test\Test1.csv"
TableToCSV(fc, csv)
Yup. Forgot to run the function. Thanks. Your my new BFF.
... View more
03-19-2013
09:20 AM
|
0
|
0
|
559
|
POST
|
How can I export a file geodatabase to csv table using python running a script from inside a map document. Got the following code from Mathew Coyle off this forum and ran it but it did not work for me. Other ideas? import arcpy from os import path as p fc = "C:\\Test\\Test.gdb\\Test1" CSVFile = "C:\\Test\\Test1" def TableToCSV(fc,CSVFile): fields = [f.name for f in arcpy.ListFields(fc) if f.type <> 'Geometry'] with open(CSVFile, 'w') as f: f.write(','.join(fields)+'\n') #csv headers with arcpy.da.SearchCursor(fc, fields) as cursor: for row in cursor: f.write(','.join([str(r) for r in row])+'\n') print 'Created %s Successfully' %p.basename(CSVFile)
... View more
03-18-2013
02:28 PM
|
0
|
2
|
1724
|
POST
|
You could futz with the fieldmappings object (you can look that up in the help), but honestly the easiest way to do what you want is to create the output table with just the two fields you want, and use Append_management with the "NO_TEST" option to copy the two fields over. The fields that do not match (by name) will be ignored in the append.
arcpy.CreateTable_management(arcpy.env.workspace,"out.dbf")
arcpy.AddField_management("out.dbf","City","Text",field_length=20)
arcpy.AddField_management("out.dbf","Zip","text",field_length=9)
arcpy.DeleteField_management("out.dbf","ID") # dummy field created by CreateTable
arcpy.Append_management("in_table","out.dbf","NO_TEST")
I figured I was going to have to do this, I was just hoping that the syntax for the Table to Table conversion was easier than I feared. Good thing there are multiple ways to do things in ArcGIS or I'd of put a bullet in my machine by now. :-). **** Added note after running script ************ Field created was actually called "field1" not "ID". ARCGIS automatically added OID field which I cannot delete (but want to) so I'm not actually getting a two field dbf but a 3 field. I have to export this dbf as a csv. Maybe I can delete the OID field then. Any hints as to how to export dbf to csv (and maybe eliminate OID field?)
... View more
03-18-2013
08:42 AM
|
0
|
0
|
370
|
POST
|
I want to copy a geodatabase table (Test) to a dbf table (Test.dbf) but am struggling with the field mapping. I have about ten fields but only want to export the City and Zip fields into the new Test.dbf file. I can get it to write all the fields to the new dbf file but am struggling with exporting just two. Help seemed to indicate this was possible but provided no examples. Here is what I have: arcpy.TableToTable_conversion("Test","C:\\TestData","Test.dbf",,(["City"],["Zip"])) What am I doing wrong????????
... View more
03-17-2013
12:34 PM
|
0
|
2
|
1429
|
POST
|
Had a eureka moment and figured it out. Correct format is: arcpy.CalculateField_management(species, "TypeName", "\"nameVariable\"", "PYTHON", "") Woohoo!
... View more
03-09-2013
06:28 PM
|
0
|
0
|
427
|
POST
|
Oops. Sorry. Copied wrong message. (That happens when you spend days on a single line of code. You get punchy and careless.) The actual message is: ExecuteError: ERROR 000539: Error running expression: nameVariable Traceback (most recent call last): File "<expression>", line 1, in <module> NameError: name 'nameVariable' is not defined Happens on last executable line before "Exit" Still stuck on this. Can't figure out how to get it to do what I want. Hopefully with the correct error message, someone can figure this out for me. I don't know where to go from here.
... View more
03-09-2013
05:20 PM
|
0
|
0
|
427
|
POST
|
Running as python script within toolbox in ArcGIS 10.1. Trying to say that if file is empty, populate the fields "TypeName","PERCENTAGE", "Region" with the following values: nameVariable, 0, "Georgia", otherwise, populate the table fields "Region" with "Georgia" and the field "TypeName" with "Goldfish". I cannot seem to populate the TypeName field with a variable. What am I doing wrong. I have spent two days on this and I cannot figure it out! # Geonet conversion put code in a single line. Below is another user's attempt to get python indentation
# fixed to make sense of the "answer". Should be good thru line 20....not sure after that.
import os.path
import arcpy
import arcgisscripting
myList = ("Fish", "Reptile","Aracnid", "Other")
geoLoc = "C:\\Data\\TempData.gdb\\"
for variablefile in myList:
# Assigning variables
species = geoLoc + (variablefile)+ "_Buffer"
# setting nameVariable
if variablefile == "Fish":
nameVariable = "Goldfish"
elif variablefile == "Reptile":
nameVariable = "Tree frog"
elif variablefile == "Aracnid":
nameVariable = "Black widow"
else:
nameVariable = "Household pet"
addRow =arcpy.da.InsertCursor(species,("TypeName","PERCENTAGE", "Region"))
result = int(arcpy.GetCount_management(species).getOutput(0))
if result == 0:
addRow.insertRow((nameVariable, 0, "Georgia"))
else:
arcpy.CalculateField_management(species, "Region", "\"Georgia\"", "PYTHON", "")
arcpy.CalculateField_management(species, "TypeName", 'nameVariable', "PYTHON", "")
#arcpy.CalculateField_management(species, "Name", "\"Goldfish\"", "PYTHON", "") Works but doesn't use variable name
exit
I get the following error message: ExecuteError: ERROR 000539: Error running expression: commonName Traceback (most recent call last): File "<expression>", line 1, in <module> NameError: name 'commonName' is not defined Pretty sure it is failing at nameVariable (bold). Thanks in advance. Question from Mar 8, 2013 6:51 PM Message was edited by: Timothy Hales Formatted Code Block Message was edited by: Timothy Hales
... View more
03-08-2013
03:51 PM
|
0
|
3
|
2586
|
POST
|
Result is not an integer you can compare how you are. You need this. result = int(arcpy.GetCount_management(Variable1).getOutput(0)) Also don't forget to add this at the end of your script. del addRow Yippee. It worked. If results isn't an integer I can compare (and I am not doubting you) why did "0" (without quotes) return for the command "print results" for the empty tables? Just trying to wrap my head around python. Thanks a zillion. I have been going around for hours on this snippet.
... View more
03-04-2013
02:12 PM
|
0
|
0
|
684
|
POST
|
Does the empty table have the fields you are trying to populate? Yes. the fields referenced in the addRow variable ("Frequency", "Used", "Region") I want to populate them with (0, "Locally", 3)
... View more
03-04-2013
12:45 PM
|
0
|
0
|
684
|
POST
|
I am using ArcGIS 10.1 and have created a script that checks to see if a file geodatabase table is empty. It is a table with no spatial referencing. The code I have thus far can add rows to tables that already have a record in it, but it will not add a new row to an empty table. Can this be done? Would it be better accomplished if it were a dbf file rather than a geodatabase table? Snippet of code looks like this: addRow =arcpy.da.InsertCursor(Variable1,("Frequency", "Used", "Region")) result = arcpy.GetCount_management(Variable1) print result print addRow if result == 0: [indent]addRow.insertRow((0, "Locally", 3))[/indent] else: [indent]do something else[/indent] result and addRow printouts are correct. Prior tests taking out the if, else statements and just running the addRow.insertRow statement showed that tables that have at least one record get appended with a new row. So I am of the opinion that I cannot add a new row using this approach. Any other ideas as to how I can add a row to an empty table. Many thanks.
... View more
03-04-2013
11:34 AM
|
0
|
5
|
3482
|
POST
|
I finally figured it out and of course it was easier than I originally made it. I just put this after the code referenced above: lyr=arcpy.mapping.ListLayers(mxd, "Two Mile Buffer", df)[0] df.extent = lyr.getExtent(True) arcpy.RefreshActiveView()
... View more
10-10-2012
10:31 AM
|
0
|
0
|
1910
|
POST
|
I have the following script to be used inside ArcMap 10.1. It prompts the user for a shape file, creates a 2 mile buffer around it and then puts the buffered shapefile into a layer group. I want to then zoom to the buffer file. I tried to use pan to selected but since since I do not really have the buffer file selected, it is not working. Anybody know how to zoom/pan to a layer using python? Below is what I have working so far. I just need the code to zoom/pan to the extents of the "Two Mile Buffer" layer. Thank you in advance Claudine import os.path import arcpy mxd = arcpy.mapping.MapDocument("CURRENT") dirPath = os.path.dirname(mxd.filePath) arcpy.env.overwriteOutput = True df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0] tocLayer = arcpy.mapping.ListLayers(mxd, "Buffer Layers", df)[0] project = arcpy.GetParameterAsText(0) buffer_shp = dirPath + "\\buffer.shp" arcpy.Buffer_analysis(project, buffer_shp, "2 Miles", "FULL", "ROUND", "ALL", "") twoMileBuffer_shp = arcpy.mapping.Layer(buffer_shp) twoMileBuffer_shp.name = "Two Mile Buffer" arcpy.mapping.AddLayerToGroup(df, tocLayer, twoMileBuffer_shp, "AUTO_ARRANGE")
... View more
10-10-2012
08:37 AM
|
0
|
2
|
7946
|
Online Status |
Offline
|
Date Last Visited |
11-11-2020
02:23 AM
|