|
POST
|
Since you only have one field value to insert, try writing the insert cursor part like this: rows = arcpy.InsertCursor("Table_2")
for item in tbList:
row = rows.newRow()
row.setValue("FieldA", item) # may need to format "item"
rows.insertRow(row)
... View more
01-30-2018
10:15 AM
|
0
|
0
|
1790
|
|
POST
|
In the field list (commented out) you have "BatchID" and in your code you are testing with "if batch_id == ..." If these are field names, they are not the same field. Did you mean them to be the same?
... View more
01-30-2018
09:15 AM
|
0
|
0
|
4795
|
|
POST
|
I believe that with an insert cursor, the field list used at line 7 should include all the field names. Assuming your table only has one field (FieldA), the list can be only 1 item. In line 10, the "None" was the value to assign to the "MyCalc" field. Since it is not part of the field list in line 7, it should be dropped. Again, if only one field, something like : inCursor.insertRow((row)) # field values to insert Where the values in tbList what you were expecting?
... View more
01-30-2018
09:06 AM
|
0
|
6
|
1790
|
|
POST
|
Usually the error will also indicate a line number and the module where the script had problems. The complete error message block would help. In the meantime, you might try something like: LYR_UPDATE_TABLE = r"C:\Path\to\file.gdb\Layer_Update"
fld = 'LastUpdate'
btch_num = raw_input("Please enter a valid Batch ID: ") #assuming you are outside arcmap
where_clause = "batch_id = '{}'".format(btch_num) # where will limit to matching rows
with arcpy.da.UpdateCursor (LYR_UPDATE_TABLE, fld, where_clause) as dt_cursor:
for row in dt_cursor:
row[0] = datetime.datetime.strftime(datetime.date.today(),"%Y-%m-%d")
dt_cursor.updateRow(row) I am assuming you are using a file geodatabase, and working outside ArcMap. You can limit the rows in the cursor with a where clause. Line 11 shows how you can format your date. Hope this helps. Edit: For the where clause, if the field type is numeric, omit the single quotes around the field value: where_clause = "batch_id = {}".format(btch_num) # where will limit to matching rows
... View more
01-29-2018
09:21 PM
|
1
|
3
|
4795
|
|
POST
|
If you want to get the distinct values from Table_1.FieldA and insert them into a new, empty table in FieldA, you can use a search cursor to retrieve the values and an insert cursor to put them in the new table. Perhaps something like: tbList = [] # empty list
for row in arcpy.da.SearchCursor('Table_1', ["FieldA"]):
if row[0] not in tbList:
tbList.append(row[0]) # copy distinct rows to list
inCursor = arcpy.da.InsertCursor('Table_2', ["FieldA","MyCalc"]) # list of all fields in new table
for row in tbList:
inCursor.insertRow((row,None)) # field values to insert
del inCursor You can create your calculation field ("MyCalc" for example) when you create your table or add it later. Hope this helps.
... View more
01-29-2018
08:24 PM
|
1
|
8
|
3070
|
|
POST
|
You might check out: Turbo Charging Data Manipulation with Python Cursors and Dictionaries. I think Example 2 might work for you.
... View more
01-29-2018
03:09 PM
|
1
|
1
|
3070
|
|
POST
|
In the last version a list was used, not a dictionary, as the goal was to use field data in creating a table name. A dictionary works with a key and value (like the traditional dictionary links a word and definition). But it is also possible to create a dictionary while scanning the field data as was done in the second example. Code (untested) would look something like: tblDict = {} # empty dictionary
for row in arcpy.da.SearchCursor(intable, "ContestTitle"):
if row[0] not in tblDict.keys():
tblDict[row[0]] = ' '.join(re.sub('[^0-9a-zA-Z ]', '', row[0].strip()).split()).replace(" ","_") So, using either a list or dictionary would work. In this case a list seemed a bit simpler. Hope this helps.
... View more
01-29-2018
09:03 AM
|
1
|
0
|
3334
|
|
POST
|
In this line of code arcpy.MakeFeatureLayer_management(lyr, FeatureLyr, dateSearch) dateSearch looks to be a where clause, but I don't see where/how it is getting initialized. Are you using something like: # for file geodatabase
dateSearch = "dateField > date '{}'".format(datetime.datetime.strftime(report_time,"%Y-%m-%d %H:%M:%S"))
# result: "dateField = date '2018-01-21 19:01:29'" The format of the where clause will vary depending on the database you are using. See SQL reference for query expressions used in ArcGIS.
... View more
01-28-2018
08:29 PM
|
1
|
0
|
1609
|
|
POST
|
Looks like you are on the right track. You can use ApplySymbology outside with IDLE; what type of error do you get when using IDLE. Here's a script that I use to apply symbology using a layer file: import os
import arcpy # if outside ArcMap
# inside ArcMap, use "CURRENT" for document name
mxd = arcpy.mapping.MapDocument(r"C:\Path\To\SymbolTest.mxd")
# layer file is in directory with mxd map document; this will be the workspace
arcpy.env.workspace = os.path.dirname(mxd.filePath)
# dictonary matches map layer with symbology layer file
# layer name in TOC : name of layer file (can use full path/file name if not in map directory)
symbols = {'SymbolTest':'LayerSymbology.lyr'}
for layer in arcpy.mapping.ListLayers(mxd):
if layer.name in symbols:
print "Layer: '{}' - previous symbology type: '{}'.".format(layer.name, layer.symbologyType)
# http://desktop.arcgis.com/en/arcmap/latest/tools/data-management-toolbox/apply-symbology-from-layer.htm
# ApplySymbologyFromLayer_management (in_layer, in_symbology_layer)
print "Applying symbology to layer '{}' using '{}'.".format(layer.name, symbols[layer.name])
arcpy.ApplySymbologyFromLayer_management(layer, symbols[layer.name])
print "Layer: '{}' - new symbology type: '{}'.".format(layer.name, layer.symbologyType)
else:
print "Layer '{}' not updated.".format(layer.name)
# may need to refresh map
# arcpy.RefreshTOC()
# arcpy.RefreshActiveView()
del mxd
... View more
01-27-2018
10:36 AM
|
2
|
0
|
2603
|
|
POST
|
UPDATED: I did a quick test of this, so it should work. For a file geodatabase, to match a single quote in a field, you escape the single quote with a second one (not \' ); I've made a correction in the code. It should now replace the single quote in the where_clause with an escaped one and remove any non-alphanumeric characters, etc. for table name. I also added a couple of lines (20-21) to delete tables if found; you may or may not want to do this. import arcpy
from arcpy import env
import re
env.workspace = r"Path\to\filedatabase.gdb"
intable = "ElectionResults_Nov2016"
tbList = []
for row in arcpy.da.SearchCursor(intable, "ContestTitle"):
if row[0] not in tbList:
tbList.append(row[0])
for tbl in tbList:
where_clause = "ContestTitle = '{}'".format(tbl.replace("'","''"))
tbl_fmt = ' '.join(re.sub('[^0-9a-zA-Z ]', '', tbl.strip()).split()).replace(" ","_")
if arcpy.Exists(tbl_fmt):
arcpy.Delete_management(tbl_fmt) # this will delete the table if it exists
arcpy.TableSelect_analysis(intable, tbl_fmt, where_clause)
... View more
01-25-2018
02:29 PM
|
1
|
2
|
3334
|
|
POST
|
For table names from the field data, you would need to replace anything that wasn't a letter, number or underscore. As an alternate, you might take the last word in the field data by using split on the last space. tbls_fmt = tbls.split(" ")[:-1] The error you mentioned occurred when a single quote was not escaped in the where clause: where = "SELECT * FROM ElectionResults_Nov2016 WHERE ContestTitle = 'STATE'S ATTORNEY'"
# should be
where = "SELECT * FROM ElectionResults_Nov2016 WHERE ContestTitle = 'STATE''S ATTORNEY'"
flds = "STATE'S ATTORNEY" # single quote inside double quotes, escape not needed
ContestTitle = flds.replace("'", "''") # for file geodatabase, use 2 single quotes Hope this helps. EDIT: When using a where statement to find a value in a file geodatabase with an apostrophe, escape the single quote by adding a second one.
... View more
01-25-2018
01:26 PM
|
1
|
0
|
3334
|
|
POST
|
One quick idea (but there's probably one better): import collections
table = [
[11000, 1],
[12000, 0],
[13000, 1],
[11000, 0],
[11000, 0],
[11000, 0],
[12000, 0],
[12000, 1],
[13000, 1]
]
d = {} # dictionary for counting
for row in table:
dictValue = "{}:{}".format(row[0],row[1])
# print dictValue
if dictValue not in d.keys():
d[dictValue] = 1 # insert key into dictionary and set value to 1
else:
d[dictValue] += 1 #increment value in dictionary
od = collections.OrderedDict(sorted(d.items()))
for k, v in od.iteritems():
print k.split(':')[0], k.split(':')[1], v
''' Results:
11000 0 3
11000 1 1
12000 0 2
12000 1 1
13000 1 2
'''
... View more
01-25-2018
09:55 AM
|
1
|
0
|
2483
|
|
POST
|
Although you can do it like that, I think Dan Patterson's suggestion of using "include" is better. Examples: The include file: # file is named "fieldcalc.py"
def Strike(a, b):
return "{}, {}".format(a,b)
def AnotherFunct(a, b):
return "{} not {}".format(a,b)
Use it like this: import fieldcalc
w = "Hello"
x = "world"
y = 3
z = 1
print fieldcalc.Strike(w,x)
# Hello, world
print fieldcalc.AnotherFunct(y,z)
# 3 not 1
Or: from fieldcalc import Strike, AnotherFunct
w = "Hello"
x = "world"
y = 3
z = 1
print Strike(w,x)
# Hello, world
print AnotherFunct(y,z)
# 3 not 1
... View more
01-24-2018
11:36 AM
|
0
|
0
|
1966
|
|
POST
|
The same question is in this thread: Is there an ArcGIS REST API endpoint to view credit balance? Although the first response is not marked as correct, it worked for me.
... View more
01-24-2018
10:21 AM
|
0
|
0
|
3709
|
|
POST
|
Try: where_expression = " FIELD2 = 'CODE1' or FIELD2 = 'CODE2' " Field names in a where clause are usually not in quotes. And with file geodatabase, use outside double quotes; inside text field values in single quotes.
... View more
01-23-2018
05:05 PM
|
3
|
1
|
9007
|
| Title | Kudos | Posted |
|---|---|---|
| 1 | 10-27-2016 02:23 PM | |
| 1 | 09-09-2017 08:27 PM | |
| 2 | 08-20-2020 06:15 PM | |
| 1 | 10-21-2021 09:15 PM | |
| 1 | 07-19-2018 12:33 PM |
| Online Status |
Offline
|
| Date Last Visited |
02-12-2026
07:13 PM
|