Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I'm having a problem copying data from a shapefile to a feature class using an insert cursor. Here is the part that is causing trouble:

newRows = arcpy.InsertCursor(Target)
sourceRows = arcpy.SearchCursor(Christiansburg)

for source in sourceRows:
    neue = newRows.newRow()
    neue.setValue(TName, source.getValue(CName))
    newRows.insertRow(neue)

It takes data from a field in the source row, and then copies it to the new row. The trouble is, when the source row has a forward slash in it, it fails when I try to do the insert, saying "The row contains a bad value.". If I edit the forward slash out of the source data, it works fine. If I replace the variable with a hardwired string that has a forward slash in it, it also works fine, so I am not 100% sure that it is the forward slash causing the problem. I have tried using the str function on the result of getValue, and it still doesn't work. The field being copied from is street names, and the row causing trouble is a street that changes name. If worst comes to worst, I could simply edit out the forward slash permanently, but I would rather not if I can help it.

Does anyone know what I am doing wrong here?

EDIT: Derped on copy/paste of code snippet. Should be fixed now.

share|improve this question
    
Do you have nulls? What version of ArcGIS are you on? –  Jason Scheirer Aug 7 '12 at 20:35
    
There are nulls in other fields, however, there are none in field that is causing trouble, either in the feature class or shapefile. Have 10.0 –  fomafomich Aug 7 '12 at 20:40
    
ahmm shouldnt you use: newRows.insertRow(neue) after you are finished iterating sourceRows? setValue() is filling just one field in a row or multiple fields simultaneously? –  Gero Aug 7 '12 at 20:53
    
As I understand it, it needs to be done for each row. setValue fills a single field in a single row. So you create a row object, fill it with setValue as needed, insert the row into the table, and move on to next source row, hence the loop. I am actually using other setValue statements for other fields in my full code, but only what I have posted is causing problems. Even that is not consistently problematic, since it only seems to cause problems when the source row has a '/' in it, and I can't figure out how to fix it. –  fomafomich Aug 7 '12 at 22:36
    
Maybe arcpy think the street name is a filepath. Why don't you try to add a backslash () in the value so it sees it as a 'raw' string e.g. from 'old name/new name' to old name\/new name'. Worths a try... –  mapoholic Aug 8 '12 at 7:07
show 2 more comments

1 Answer 1

up vote 2 down vote accepted

I figured out what the problem was. Turns out, the field in the target feature class has a field length of 35 characters, while the corresponding field in the shapefile has a field length of 256 characters. The row that was causing problems was exactly 36 characters long, and was the only one that had a '/'. So when I deleted the '/' that brought the length to 35 characters and it worked. I suspect I spelled something wrong when I hardwired the string, so that is probably why that worked then. I truncated the row to 35 and it works like a charm, even with the '/'. So that must have been the problem. Thanks to all who took the time to reply and think about it, especially mapoholic. I noticed this testing his suggestion. I guess the moral of the story is to make sure that your fields are the right length when doing this sort of thing.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.