Read and Write to a CSV Content Item in ArcGIS Online using the ArcGIS API for Python

Table of Contents

Introduction

The ArcGIS API for Python is a powerful Python library that allows users to interact with and automate tasks in ArcGIS Online (or Portal). The API is excellent for programmatically creating, maintaining, and updating components of ArcGIS Online such as content items. In this post we will use the ArcGIS API for Python to update/edit a CSV content item directly in ArcGIS Online without having to download the CSV file to disk first and re-upload. In our previous blog post we looked at creating a CSV content item directly in ArcGIS Online, this is a follow-up.

Unlock the full potential of ArcGIS Online by mastering the art of efficient Content Management with the ArcGIS API for Python. In this comprehensive course, you will embark on a journey to streamline your geospatial workflows, enhance data organization, and maximize the impact of your ArcGIS Online platform.

Geospatial Professionals, GIS Analysts, Data Managers, and enthusiasts will discover the power of automation and script-based operations to efficiently manage content in ArcGIS Online. Throughout this course, you will gain practical, hands-on experience in leveraging the ArcGIS API for Python to perform a wide range of content management tasks with ease.

arcgis modules and standard Python csv

The API provides access to your organisations ArcGIS Online via the GIS class in the gis module. This GIS class is the gateway to ArcGIS Online. 

				
					## import GIS which provides the gateway entry to your AGOL
from arcgis.gis import GIS
## import the standard Python csv module
import csv

				
			

Accessing ArcGIS Online

Our first port of call is to access your ArcGIS Online via the GIS class. There are a handful of ways to achieve access, if you are logged into your ArcGIS Online in ArcGIS Pro you can simply use "home", otherwise, another common way is to provide the ArcGIS Online URL, followed by your username and password. This is handled in the code snippet above.

				
					## Access AGOL
agol = GIS("home")
				
			
				
					## Access AGOL
agol = GIS(
    url = "https://your_organisation.maps.arcgis.com/",
    username = "Your_Username",
    password = "Your_Password"
)
				
			

Get CSV item object

Use the ContentManager get() method to access the CSV item object.

				
					## get CSV item object
csv_item = agol.content.get("CSV_ITEM_ID")
				
			

Get the CSV filepath

Get the CSV filepath which is necessary to read and write to a CSV.

				
					## get the csv path
csv_filepath = csv_item.get_data()
				
			

Print the first few rows

Let’s take a look at the first 5 rows of the CSV. We can see we have Shapefile as a TYPE, we are only interested in Feature Services and WebMaps

				
					## print first five rows of the csv
with open(csv_filepath, "r") as file:
    csv_reader = csv.reader(file)
    for row in [row for index, row in enumerate(csv_reader) if index < 5]:
        print(row)
				
			

Get a unique set of item types

Let’s check all the TYPEs in the CSV.

				
					## get the item types
with open(csv_filepath, "r") as file:
    csv_reader = csv.reader(file)
    next(csv_reader)
    item_types = set([row[1] for row in csv_reader])
    
print(item_types)
				
			

Get all rows as a list of lists

We get the current CSV rows as lists within a list. The headings are the first entry in our lest so we will pop those for future use.

				
					with open(csv_filepath, 'r') as read_csv:
    reader = csv.reader(read_csv)
    data = list(reader)
    
headings = data.pop(0)
				
			

Remove unwanted rows

Iterate through our list of lists removing any list (row) that is not Feature Service or Web Map.

				
					updated_data = [headings]
for row in data:
    if row[1] in ("Feature Service", "Web Map"):
        updated_data.append(row)

del data
				
			

Rewrite the csv and update the item object

We now overwrite the CSV data with the updated data. We use the item object update() method from the ArcGIS API for Python to commit the update to the CSV item.

				
					with open(csv_filepath, 'w') as write_csv:
    writer = csv.writer(write_csv)
    writer.writerows(updated_data)

del updated_data

csv_item.update(data=csv_filepath)
				
			

Check the update

Let’s re-call the CSV item and check that the updates worked.

				
					csv_item = agol.content.get("CSV_ITEM_ID")
csv_filepath = csv_item.get_data()
with open(csv_filepath, "r") as file:
    csv_reader = csv.reader(file)
    for row in csv_reader:
        print(row)
				
			

Perfect! We only have Feature Service and Web Map items in our CSV!

Unlock the full potential of ArcGIS Online by mastering the art of efficient Content Management with the ArcGIS API for Python. In this comprehensive course, you will embark on a journey to streamline your geospatial workflows, enhance data organization, and maximize the impact of your ArcGIS Online platform.

Geospatial Professionals, GIS Analysts, Data Managers, and enthusiasts will discover the power of automation and script-based operations to efficiently manage content in ArcGIS Online. Throughout this course, you will gain practical, hands-on experience in leveraging the ArcGIS API for Python to perform a wide range of content management tasks with ease.

All the code in one place

You can find the entire code workflow below.

				
					from arcgis.gis import GIS
import csv

agol = GIS("home")

## get CSV item object
csv_item = agol.content.get("CSV_ITEM_ID")

## get the csv path
csv_filepath = csv_item.get_data()

## print first five rows of the csv
with open(csv_filepath, "r") as file:
    csv_reader = csv.reader(file)
    for row in [row for index, row in enumerate(csv_reader) if index < 5]:
        print(row)

## get the item types
with open(csv_filepath, "r") as file:
    csv_reader = csv.reader(file)
    next(csv_reader)
    item_types = set([row[1] for row in csv_reader])
print(item_types)

## get rows as a list of rows, pop the headings
with open(csv_filepath, 'r') as read_csv:
    reader = csv.reader(read_csv)
    data = list(reader)
    
headings = data.pop(0)

## remove unwanted rows
updated_data = [headings]
for row in data:
    if row[1] in ("Feature Service", "Web Map"):
        updated_data.append(row)

del data

## rewrite csv data based on updated list
with open(csv_filepath, 'w') as write_csv:
    writer = csv.writer(write_csv)
    writer.writerows(updated_data)

del updated_data

## commit the changes to the csv
csv_item.update(data=csv_filepath)

## check the update worked
csv_item = agol.content.get("CSV_ITEM_ID")
csv_filepath = csv_item.get_data()
with open(csv_filepath, "r") as file:
    csv_reader = csv.reader(file)
    for row in csv_reader:
        print(row)

				
			

Leave a Comment

Your email address will not be published. Required fields are marked *