0

I have a folder with a (3) shapefiles. Each shp. has a field titled "country" with a country name. I've already set my desired workspace correctly and everything else in my script works for what I want to do, it's just this part I'm struggling with. What I'm attempting to do is to slice the country name so only the first letter of the country is displayed in the country field for each attribute. So for example, if my first attribute in the country field is Canada, I want to see 'C' instead, if my second row is Portugal, I want to see 'P' etc. I need the loop to go though each shapefile in my folder as you can see. I generally don't have much experience at all using string slicing or string concatenation. My desire is to use an update cursor to accomplish this.

Below is a snippet of my current script attempting to accomplish this.

   try:
       for fc in arcpy.ListFeatureClass():
           with arcpy.da.UpdateCursor(fc,["country"]) as cursor:
               for row in cursor:
                   row[0] = [0]
                   cursor.updateRow(row)
               del row, cursor
   except:
       print ("Could not complete the string slicing notation.")
  • 1
    Please include your code as formatted text rather than as screenshot – MrXsquared Oct 4 '19 at 19:13
  • If you need help with how to slice a string that pure Python question should be researched/asked at Stack Overflow. – PolyGeo Oct 4 '19 at 19:55
  • When testing and presenting code snippets here I recommend removing any try/except statements because they can mask otherwise useful error messages. – PolyGeo Oct 4 '19 at 21:48
3

Currently, the loop should be creating a row value of a list containing the number 0, which would probably error out. I think the deletion of row and cursor are also unnecessary as you have a with statement which should close out of the cursor properly.

If you want just the first item of a string value from the first element in the row, calculate it as

row[0] = row[0][0]

This is equivalent to:

country = row[0]
row[0] = country[0]

Updating your code with the first option above:

try:
    for fc in arcpy.ListFeatureClasses():
       with arcpy.da.UpdateCursor(fc,["country"]) as cursor:
           for row in cursor:
               row[0] = row[0][0]
               cursor.updateRow(row)
except:
    print ("Could not complete the string slicing notation.")
  • Awesome, thanks. If I had another field that i wanted to do the same thing in another try/except - but the values in my field were numbers (TEXT field type as well though), would this require different formatting for the numbers? – Connor Garrett Oct 4 '19 at 20:31
  • @ConnorGarrett This is another question which should be asked separately. If the answer solved your question, please accept it. – ahmadhanb Oct 5 '19 at 7:31
  • If the field is text the slice should be fine, otherwise you'd need to convert the value of row[0] to string first. – smiller Oct 5 '19 at 13:23
  • Hello,I went ahead and tried this but the values are stills showing how they originally were before. row[0] = row[0][0] doesn't change the output when I run the script. – Connor Garrett Oct 7 '19 at 13:37
  • Please verify you are getting results from arcpy.ListFeatureClasses() (note: changed from ListFeatureClass() which was a typo copied over from your code above). – smiller Oct 7 '19 at 14:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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