I am writing a Python script that finds the difference between populations for U.S. counties from one year to another and writes that information to a new field in a xlxs
file.
Can it be done with ArcPy and what would the code look like?
Below is a sample of the xlxs data with the column names:
Area_name, Resident population 1990, Resident population 2000, Resident population 2010
UNITED STATES, 248709873, 281421906, 308745538
ALABAMA, 4040587, 4447100, 4779736
Autauga, AL, 34222, 43671, 54571
Baldwin, AL, 98280, 140415, 182265
Barbour, AL, 25417, 29038, 27457
Bibb, AL, 16576, 20826, 22915
I have the below code so far but I'm getting errors:
import arcpy
from arcpy import env
import xlrd
env.workspace=r'C:\Users\Kevin\Desktop\Geoprograming\week_four'
workbook = xlrd.open_workbook('test.xlsx')
worksheet = workbook.sheet_names('sheet1')
cursor = arcpy.da.UpdateCursor(population, ["1990", "2000"])
cursor.insertRow([diff90_10])
for row in cursor:
row[diff90_10] = row[2010]-row[1990]
cursor.updateRow([row])
Runtime error Traceback (most recent call last): File "", line 9, in File "C:\Python27\ArcGIS10.4\lib\site-packages\xlrd__init__.py", line 394, in open_workbook f = open(filename, "rb") IOError: [Errno 2] No such file or directory: 'test.xlsx'
env.workspace
is only aware inarcpy
. If you want to use it with other modules, you can useos.chdir(env.workspace)
or pass in the absolute path totest.xlsx
.