6/16/2020 Exercise: Create data types Print Window
Exercise: Create data types
The city of San Diego has a folder of newly created shapefiles. Because the shapefiles are new, the GIS
department needs to have a text file of these shapefiles added to the New Data folder so that they can be
reviewed. However, some of these shapefiles do not fit the standard naming template (<data>_SD.shp).
As data manager, you have decided to write a Python script that will create a list of these shapefiles; it will
later be used to update their names.
Estimated completion time: 20 minutes
To complete exercises, you need the following:
ArcGIS Desktop 10.7 (Basic, Standard, or Advanced)
PythonWin 2.7
ArcMap users: PythonWin 2.7 is available as a free download. If you do not have PythonWin installed, the
first course exercise provides instructions for downloading the appropriate version.
- Step 1: Download the data
To complete the exercise, you must download the data. If you have already downloaded and
installed the data, continue to the next step.
- Step 2: Set up the script environment
Because you will be writing and editing this script, you will use the PythonWin IDE environment.
Start PythonWin.
Remind me how
In File Explorer, browse to C:\Python27\ArcGIS10.x\Lib\site-packages\pythonwin, and then double-click
[Link].
Tip: For easy access in the future, right-click [Link] and choose Pin To Taskbar.
In PythonWin, click the New button .
1/9
6/16/2020 Exercise: Create data types Print Window
Step 2a: Set up the script environment.
In the New dialog box, confirm that Python Script is highlighted, and then click OK to open
a new script window.
Step 2b: Set up the script environment.
Your new script is named Script1 and appears in a new script window.
Tile the script and Interactive window.
Remind me how
From the Window menu, choose Tile.
From the File menu, choose Save As.
In the Save As window, browse to your C:\EsriTraining\PythEveryone\CreatingScripts folder.
For the name of the new script, type [Link].
Click Save.
2/9
6/16/2020 Exercise: Create data types Print Window
The scripting environment is now set up. You are ready to start writing your script.
- Step 3: Write pseudocode for the script
After the script environment is set up, you will write the pseudocode. Pseudocode is a description
of the ingredients and instructions you will use in your script. It helps guide you as you write the
script and can also help debug your script.
? What ingredients will you need to create this script?
? What data types will you need to use for the script ingredients?
In the script window, type the following pseudocode:
#Assign variables to the shapefiles
Step 3a: Write pseudocode for the script.
Here, you will create variables for each of the shapefiles in the San Diego folder.
Note: The hash mark indicates that this line will not run in the script, and it will serve as a
comment, or, in this case, pseudocode.
Press Enter twice to move to line 3 of the script.
On line 3, type the following pseudocode, and then press Enter:
#Create a list of shapefile variables
Step 3b: Write pseudocode for the script.
Here, you will create a list of the variables that you will assign under line 1.
You now have descriptions of the ingredients that this script will use.
3/9
6/16/2020 Exercise: Create data types Print Window
- Step 4: Assign the variables
After writing the pseudocode, the next step is to write the script.
In the script window, click next to line 2.
Type the following:
park = "Parks_sd.shp"
This line of code in your script assigns the Parks_sd.shp literal to the variable named park.
Press Enter.
? Is the Parks+[Link] literal a string or number?
Assign Schools_sd.shp to a variable called school, and then press Enter.
Assign Sewer_Main_sd.shp to a variable named sewer, and then press Enter.
Tip: If you are not sure what the script should be, then look for the
SanDiegoShapefiles_temp.py file located in the
..\EsriTraining\PythEveryone\CreatingScripts folder.
You now have variables for each of the shapefiles.
- Step 5: Concatenate the variables
Scripts often need to combine and manipulate variables. One method used to manipulate
variables is concatenation. Concatenation uses the addition operator, +, to combine strings or
string variables. In this step, you will recreate the park shapefile using concatenation.
In the Interactive window, type the following line of code:
parkName = "Parks_sd"
Press Enter.
In the Interactive window, pressing Enter runs the line of code that you just wrote.
Type and run the following code:
4/9
6/16/2020 Exercise: Create data types Print Window
print (parkName)
print writes the string value of the variable parkName. This variable is only part of the shapefile
name, so you will need a few other variables to manipulate.
In the Interactive window, assign the shapefile extension, .shp, to a variable named
shapeExt.
Next you will use the Python + operator to combine the two variables. The + operator will join, or
concatenate, the strings values.
In the Interactive window, type and run the following line of code:
parkName + shapeExt
Step 5a: Concatenate the variables.
Note: If you have two numbers, whether literals or variables, and use the Python + operator,
the numbers will be added, not concatenated like strings.
Concatenating the parkName and shapeExt variables created a new string that was almost the
full name of the sd_park520.shp in the San Diego folder. You need to add only the 520 to
complete the shapefile variable.
Assign the number 520 to a variable named parkZip, and then press Enter.
Step 5b: Concatenate the variables.
Type and run the following line of code (it will return an error):
parkName + parkZip + shapeExt
5/9
6/16/2020 Exercise: Create data types Print Window
Step 5c: Concatenate the variables.
The red Traceback message explains that you have encountered an error with this line of code.
? Why can you not successfully concatenate the parkName, parkZip, and shapeExt variables?
In order to successfully concatenate these variables, you need to convert the parkZip value to a
string. There are several ways to do this conversion. One option is to reassign the shapeExt
variable and enclose 520 within the quotation marks. The other is to use the str() function,
which tells Python to use the string equivalency of the value inside the parentheses.
Type and run the following code:
park = parkName + str(parkZip) + shapeExt
Print the park variable.
Step 5d: Concatenate the variables.
Successful concatenation of string and integer file name parts.
You successfully recreated the park variable by using concatenation to manipulate variable values.
- Step 6: Index and slice the variables
Other methods used to manipulate variables include indexing and slicing. In this step, you will
recreate the school variable using a combination of concatenation, indexing, and slicing.
In the Interactive window, type and run the following line of code:
park[0]
6/9
6/16/2020 Exercise: Create data types Print Window
Step 6a: Index and slice the
variables.
In this example, you indexed the first character in the string Parks_sd520.shp, which is the
character "P".
? What line of code would you use to return the "k"?
You can also index characters from right to left by starting with -1 and moving left.
Type and run the following line of code:
park[-1]
Step 6b: Index and slice the variables.
Slicing can be used to return multiple characters by indicating a starting and ending point.
In the Interactive window, type and run the following line of code:
park[8:11]
Step 6c: Index and slice
the variables.
The colon separates the starting index from the ending index. Python will return the characters
from the starting point up to the ending point.
Note: [: and :] are used when you want to include the beginning and ending characters
in the returned value.
? What line of code would you use to return ".shp"?
7/9
6/16/2020 Exercise: Create data types Print Window
In the Interactive window, assign "Schools_sd454" to a variable named schoolName.
Use slicing and concatenation to combine schoolName with the park's shapefile
extension (".shp") and assign it to a variable named school.
Print school to verify that it returns Schools_sd454.
schoolName = "Schools_sd454"
school = schoolName + park[-4:]
print school
Step 6d: Index and slice the variables.
These examples are a few examples of variable manipulation using concatenation, indexing, and
slicing.
- Step 7: Create a list
The script's next task (indicated in the pseudocode) is to create a list of shapefile variables.
In the script window, under #Create a list of shapefile variables, move your
cursor to line 7.
Step 7a: Create a list.
Type the following line of code:
shapeList = [park, school, sewer]
Press Enter.
8/9
6/16/2020 Exercise: Create data types Print Window
Step 7b: Create a list.
In this line of code, shapeList is a variable assigned to a list of values. The list is indicated with
the bracket closures [], and each item in the list is separated by a comma.
? Why would you use a list instead of a tuple?
? How would you change the shapeList variable to create a list of the shapefile literals,
instead of their variables?
Like strings, lists can be indexed. Each item in the list has an index value—starting with 0, moving
left to right.
The ingredients have been included in the script. You will use this script later to update the list of
shapefile variables.
Save the script.
If you have time to complete the next exercise, keep the script open in PythonWin;
otherwise, exit PythonWin.
9/9