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
  • 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
    Commented Apr 7, 2021 at 23:12
  • OKay, let me re-edit it. Thanks Commented Apr 7, 2021 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
    Commented Apr 8, 2021 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
    Commented Apr 8, 2021 at 3:29
  • This post was looking for help with a graded assignment in Penn State University's GEOG 485: GIS Software Programming class. If you're a current student in this class, please be aware that the class instructors and grading assistants are aware of this post. Attempting to pass off the code found here (or code with superficial differences) as your own will make you subject to the class academic integrity policy (i.e., we consider it cheating). Commented Feb 21, 2022 at 21:41

1 Answer 1

1

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 and acknowledge you have read our privacy policy.

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