0

I am working on a homework assignment for my coding class, and I am having issues with the last part of the assignment. My job is to create new three shapefiles based on players' positions in a target country. The target country is Sweden, and the positions that we are creating the individual shapefiles from are:

  • Left-Wing (LW)
  • Right Wing (RW)
  • Center (C)

The two fields are the weight and height field, and the weight field is in pounds, and the height field is in inches, and the height field is given in the text in the original shapefile (nhlrosters. hp). The new shapefiles would have the height and weight field, but the height needs to be in cm and kg.

The data for this lesson is located at: https://www.e-education.psu.edu/geog485/node/276

How can I manipulate the string for the height field? Below is the code for the weight field as an example.

import arcpy

fc = r"\\C\\Data":

oldField = "weight"

newField = "weight_kg"

arcpy.AddField_management(fc, newField, "FLOAT")

with arcpy.da.UpdateCursor(fc, (oldField, newField)) as weightCursor:

      for weightRow in weightCursor:

           old_Weight = weightCursor[0]

           new_Weight = old_Weight * 0.453592

           weightRow[1] = new_Weight

           weightCursor.updateRow(weightRow)
                     

For the height column. The height are in feet/inches (example: 6'2")

5
  • 1
    While testing I think it's best to leave out try/except statements because they can mask otherwise useful error messages. – PolyGeo Apr 7 at 23:00
  • What happens when you run the code that you have presented? I think you should try to include just a a code snippet rather than your whole code so that we can try to help you with where you are stuck rather than with your whole code. – PolyGeo Apr 7 at 23:12
  • OKay, let me re-edit it. Thanks – buster Williams Apr 7 at 23:57
  • 1
    I do not think this is GIS but parsing/conversion via python related question and this answer may provide some insights. – fatih_dur Apr 8 at 0:28
  • If you only have one cursor at a time, it is simplest to name the cursor "cursor" and the row "row", at which point you can compare your usage with the half-skillion other cursor samples, and notice that you are accessing the cursor for row information. Note that when you are first learning Python is the time to start using best practice, which doesn't include using uppercase characters in variables where the words are separated by underscores (old_weight not old_Weight). – Vince Apr 8 at 3:29
0

It gets a bit tricky with both types of quotes in your strings. But you can use re module to extract the numbers as a list:

import re

fc = r"C:\GIS\data\testdata\project3_2021\nhlrosters.shp"
oldheight = 'height'
newheight = 'heightcm'

with arcpy.da.UpdateCursor(fc, [oldheight, newheight]) as cursor:
    for row in cursor:
        feet, inches = [int(val) for val in re.findall(r'\d+', row[0])]
        newh = feet*30.48 + inches*2.54
        row[1] = newh
        cursor.updateRow(row)

enter image description here

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.