|
POST
|
Not sure why you would get that error. Are you sure 50 characters is long enough? I think you would see a different error if that were the issue though... Actually I think I may have missed the last bracket in the last line I posted. insertCursor.insertRow((key, ', '.join(str(item) for item in val))) If that modification nets the same error can you post the code you are running?
... View more
02-07-2013
12:45 PM
|
0
|
0
|
1927
|
|
POST
|
You need to create your target table with the field names and properties you want. You could modify the insertCursor object creation to have different field names if you'd like, but yes they will have to created already.
... View more
02-07-2013
11:32 AM
|
0
|
0
|
1927
|
|
POST
|
It would be easier to answer if you gave a larger sample of the kinds of values in your field you want to split. Do they always end in Street/Avenue etc? Something like this may work for you. var.rsplit(' ', 1)[0]
... View more
02-07-2013
11:13 AM
|
0
|
0
|
1459
|
|
POST
|
Oh yes I had forgotten it was a list of ints you were pulling here. Try replacing this as the last line. The 'for key, val...' line is there just for reference it is the same.
for key, val in tableDict.iteritems():
insertCursor.insertRow((key, ', '.join(str(item) for item in val))
... View more
02-07-2013
11:00 AM
|
0
|
0
|
2761
|
|
POST
|
Can you open it in ArcCatalog? Or are you leaving it open somewhere while it is trying to be accessed? I'd recommend closing any open ArcGIS instances before running it to make sure.
... View more
02-07-2013
10:27 AM
|
0
|
0
|
2761
|
|
POST
|
Don't open the table that way, that is for a different kind of accessing. The cursor handles all the back end you need. Your outPath variable should be to a place on disk, not in memory. Your new_table variable should just be the name of the table not the path. Try this. import os
import arcpy
table = r"c:\users\craig.mcdade\GridforPython.dbf" # source_table
new_table = r"table.dbf" # new table name + extension only
tempTable = r"\\in_memory\temp_table"
outPath = r"c:\users\craig.mcdade" # path to output location on disk
keyField = 'NAME'
valField = 'Pg_Number'
tableDict = {}
arcpy.MakeTableView_management(table, tempTable)
with arcpy.da.SearchCursor(tempTable, [keyField, valField]) as cursor:
for row in cursor:
tableDict.setdefault(row[0], []).append(row[1])
insertCursor = arcpy.da.InsertCursor(os.path.join(outPath, new_table), [keyField, valField])
for key, val in tableDict.iteritems():
insertCursor.insertRow((key, ', '.join(val)))
... View more
02-07-2013
09:30 AM
|
0
|
0
|
2761
|
|
POST
|
Here is one way of making sure each fc field in the list actually has a corresponding field in the data. field_name_list = [field.name for field in arcpy.ListFields(fc) if field.name not in field_list] I'd also add 0 to your val_list since you test for it each time.
... View more
02-07-2013
08:32 AM
|
0
|
0
|
937
|
|
POST
|
The insert cursor is a little different than the search cursor. To use the .da insert cursor you want to do something like this. insertCursor = arcpy.da.InsertCursor(os.path.join(outPath, new_table), [keyField, valField])
for key, val in tableDict.iteritems():
insertCursor.insertRow((key, ', '.join(val)))
... View more
02-07-2013
06:57 AM
|
0
|
0
|
2761
|
|
POST
|
Yes because \t is a tab. You need to format paths like this when using single backslashes. new_table = r"c:\users\craig.mcdade\table.dbf" You can read this for more information on string literals. http://docs.python.org/2/reference/lexical_analysis.html#string-literals
... View more
02-07-2013
05:59 AM
|
0
|
0
|
2761
|
|
POST
|
Ah yes you'd need to declare that. Something like this. tempTable = r'\\in_memory\temp_table' Also you'll want to make it a dbf or fgdb table, something Arc can read easily. Excel is not great for that. You'll also have to create this before you run your script. Make sure the field you want to all the values in is a long enough string field.
... View more
02-07-2013
05:14 AM
|
0
|
0
|
3144
|
|
POST
|
Try changing the search cursor to these lines. tableDict = {}
arcpy.MakeTableView_management(table, tempTable)
with arcpy.da.SearchCursor(tempTable, [keyField, valField]) as cursor:
for row in cursor:
tableDict.setdefault(row[0], []).append(row[1])
... View more
02-07-2013
04:38 AM
|
0
|
0
|
3144
|
|
POST
|
That could be from your target string field you are concatenating all the values together in is not long enough. Or it could be something else. Do you have a join on your table by any chance?
... View more
02-07-2013
04:02 AM
|
0
|
0
|
3144
|
|
POST
|
I haven't tested this exactly but it should get you on the right path. It cursors through your initial table creating a dictionary of key, list values. Then it uses an insert cursor to basically inject those dictionary keys and lists into a new table using the same field names. You'd have to either create the new table or add that aspect to the script. import arcpy
table = # source_table
new_table = # new table
keyField = 'Name'
valField = 'Pg_number'
tableDict = {}
for row in arcpy.SearchCursor(table):
tableDict.setdefault(row.getValue(keyField), []).append(row.getValue(valField))
insertCursor = arcpy.InsertCursor(new_table)
for key, val in tableDict.iteritems():
row = insertCursor.newRow()
row.setValue(keyField, key)
row.setValue(valField, ', '.join(val))
insertCursor.insertRow(row)
... View more
02-06-2013
12:17 PM
|
0
|
0
|
3144
|
|
POST
|
I don't understand fully what you are trying to do. Can you give an example of the desired output for the first few rows of the data you posted?
... View more
02-06-2013
11:14 AM
|
0
|
0
|
3144
|
| Title | Kudos | Posted |
|---|---|---|
| 1 | 05-17-2011 10:36 AM | |
| 1 | 08-16-2012 10:48 AM | |
| 1 | 10-31-2012 08:39 AM | |
| 1 | 07-16-2012 01:52 PM | |
| 1 | 03-15-2012 10:57 AM |
| Online Status |
Offline
|
| Date Last Visited |
08-22-2024
11:12 PM
|