|
POST
|
I didn't think about doing this either. Interesting idea, I'll have to give it a try.
... View more
04-15-2015
11:12 AM
|
0
|
0
|
1444
|
|
POST
|
We use cx_Oracle as well. Here's a sample snippet that I use. import cx_Oracle
try:
oracle_db = u'UserNameHere/PasswordHere@DatabaseNameHere'
sql = "SELECT * FROM TABLE_NAME"
cnxn = cx_Oracle.connect(oracle_db)
sqlcursor = cnxn.cursor()
sqlcursor.execute(sql)
sqlresult = sqlcursor.fetchall()
# Validate and process result
except Exception as err:
print err
finally:
sqlresult = None
if 'sqlcursor' in locals():
sqlcursor.close()
del sqlcursor
if 'cnxn' in locals():
cnxn.close()
del cnxn The result table can be returned in different ways, so you should validate it before you try to process it. Valid SQL query but no rows returned: empty list [] Exactly one row with only one field value returned: single item tuple in a list [(val,)] More than one row returned with more than one field: list of tuples, each row being a tuple [(val, val), (val, val)] The third scenario is the most common, and what you should expect with the SQL query you posted. The number of tuples is the number of rows. The number of items in each tuple is the number of fields.
... View more
04-14-2015
12:23 PM
|
2
|
0
|
3714
|
|
POST
|
1. You can try using raw_input() 3. Use os.path to construct and deconstruct file paths.
... View more
04-10-2015
04:52 PM
|
1
|
0
|
1505
|
|
POST
|
Xander, I recently unlocked the mysteries of enumeration and noticed your cnt variable does something similar. Because I'm still learning, I thought something like this would work instead: with arcpy.da.SearchCursor(fc_house, flds_house) as curs_in:
for cnt, row_in in enumerate([i for i in curs_in], start=1):
curs_in.reset() ## Return cursor back to the first row after enumeration
if cnt % 25 == 0:
print "Processing connection: {0}".format(cnt)
pnt1 = row_in[0]
parcel_id = row_in[1]
date_sale = row_in[2]
fire_oid = row_in[3]
# Continue processing rows in cursor... After some tinkering, I realized that when you enumerate a cursor like this, it goes through all the rows. The problem is that the cursor object ends with no more rows and you have to call reset() on the cursor to start it back at the first row again. Since there is still more processing to be done, you'll essentially be iterating over all rows in the cursor twice with enumeration instead of once with your original counter variable. Do you think the extra time it takes to create the enumeration of the cursor is worth it in a case like this? Maybe only for cursors with a small number of rows?
... View more
04-09-2015
11:18 AM
|
0
|
4
|
9494
|
|
POST
|
You can copy and paste the code because it is still just text. However, keep in mind that the editor in GeoNet is a rich text editor so it will try to keep any formatting that was on the original text. So if you copy a small portion of text from a code block with syntax highlighting and paste it into your reply, it will display with the same color and font but might be missing the background that's added when it's actual syntax highlighting. So if you try to copy and paste code like that, it might look weird. I find it's better to paste it into notepad (or some text editor that strips formatting) and then copy it from there and paste it into GeoNet. Once it's back in the GeoNet editor, you can apply syntax highlighting again and it will look like it should.
... View more
04-09-2015
10:21 AM
|
0
|
0
|
1797
|
|
POST
|
Are you having an issue running this code or are you just looking for suggestions to improve your code that's already working?
... View more
04-09-2015
10:14 AM
|
0
|
1
|
2191
|
|
POST
|
Almost, but not quite. That would give you an error because there is nothing at index 1. The field_names parameter for the SearchCursor can be declared as a list directly in the call to the method or you can assign it to a variable ahead of time and then pass that variable containing the field names list as the field_names parameter; don't do both. In your example, you've created a fields variable that contains a list of field name strings. Then, when you create the cursor, you explicitly tell it to use only "SomeField". What you'd get are rows as a single item tuple with one field value for SomeField. If you instead wanted access to the field values from all three fields you specified in the fields variable, create the cursor with fields as the field_names parameter (instead of just ["SomeField"] like I did in the example just before. These two examples would both do the same thing: print the field value of SomeField for every row in the feature class. fc = r'PathToLayer\LayerNamel'
fields = ["ObjectID", "SomeField", "Area"]
with arcpy.da.SearchCursor(fc, fields) as sCursor:
for row in sCursor:
print row[1] fc = r'PathToLayer\LayerNamel'
with arcpy.da.SearchCursor(fc, ["SomeField"]) as sCursor:
for row in sCursor:
print row[0] Choose one or the other, but don't combine them both.
... View more
04-08-2015
03:21 PM
|
2
|
0
|
2041
|
|
POST
|
Yes. Since I don't know exactly what outFld contains, I'll draft an example... fc = r'PathToLayer\LayerNamel'
fields = ["ObjectID", "SomeField", "Area"]
with arcpy.da.SearchCursor(fc, fields) as sCursor:
for row in sCursor:
print row ## Prints tuple of all field values
print row[0] ## Prints single field value in first field, ObjectID
print row[1] ## Prints single field value in second field, SomeField
print row[2] ## Prints single field value in third field, Area The example here using the with statement is the more typical way you'll see the cursors being used. The example I posted earlier uses the cursor in what's called list comprehension, which uses shorthand code to create a list from an iterable. The code from James Crandall also uses list comprehension to create a list of field names.
... View more
04-08-2015
02:58 PM
|
1
|
2
|
2041
|
|
POST
|
Yes, that's correct. I think what Dan is getting at is that although ListFields returns an iterator, the field objects it's iterating can't be compared directly, you have to call the property of the field object; like with .name
... View more
04-08-2015
02:51 PM
|
2
|
1
|
2041
|
|
POST
|
It's referring to an index of the list of values in the row variable. The search cursor returns each row as a tuple (which is a kind of list in Python). The number of indexes in that tuple is equal to the number of fields you used in the arguments to create the cursor. In this case, there's only one field so it returns a tuple with only one value as (23456.93426,) The comma in there is important because a single item tuple still has a comma afterwards (unlike a list). So when I say row[0], it's getting the value of the first item, which is also known as index 0. Index 1 would be the second item, and so on. If I just said row, it would return all field values in the whole row as a tuple instead of just the single value as a number.
... View more
04-08-2015
02:40 PM
|
0
|
5
|
2041
|
|
POST
|
The regular (non da) cursors are also older and will eventually get depreciated by Esri. The new ones are more future proof.
... View more
04-08-2015
02:17 PM
|
2
|
0
|
4236
|
| Title | Kudos | Posted |
|---|---|---|
| 1 | 10-23-2025 03:53 PM | |
| 1 | 3 weeks ago | |
| 1 | 03-19-2026 08:59 AM | |
| 1 | 02-12-2026 01:37 PM | |
| 1 | 12-01-2025 06:19 AM |