Exporting results to CSV?

08-01-2022 11:06 AM
Occasional Contributor


I have a notebook within AGO that prints all the public facing content within my organization. The results are formatted to be comma delimited to make it easy to import into an excel spreadsheet to share. Is it possible to directly export the results of this script to an excel file? 



from arcgis.gis import GIS
gis = GIS("home")
from IPython.display import display

my_content = gis.content.search(query="owner:USERNAME",max_items=10000)
#item_types_need = ['Web Map','Web Mapping Application','StoryMap','Dashboard','Web Experience','Form']
item_types_need = ['Feature Service']
for item in my_content:
if item.type in item_types_need:
if item.content_status == "public_authoritative":
author = "Authoritative"
author = "None"
if item.shared_with['everyone']== True:


0 Kudos
1 Reply
Esri Contributor

Hi Dave,  thanks for asking this question.  Here is one thing you may try. 

1. At the top of the script, somewhere above the for loop, add an empty list.  Then have the remainder of your script below it.


result = []
for item in my_content:....


2. Replace the print statement with the following, and make sure you indent it accordingly after the final if statement:  .append should take the print statement and put it into a list, where each line of the print statement (with title, id, type, author) becomes an element of the list.




 3. Then use pandas to convert the list to a dataframe, however at this point the 4 pieces of information will be in one column called "to_split".


df = pd.DataFrame(result, columns=["to_split"])


 4. You then have to split the column based on the commas, and rename the new columns.


# split the column based on the comma separators, then rename the new columns
# https://datascienceparichay.com/article/pandas-split-column-by-delimiter/#:~:text=Split%20column%20by%20delimiter%20into,True%20to%20the%20expand%20parameter.

# split column and add new columns to df
df[['item_name', 'item_id', 'item_type', 'author']] = df['to_split'].str.split(',', expand=True)


5. Last, convert it to a CSV file, keeping the columns you want.  Because you are using an AGOL notebook, you have to save it to the user workspace.  You can then navigate to the files tab and download the CSV locally.


# convert to csv file
df.to_csv("/arcgis/home/test_output_v2.csv", columns=['item_name', 'item_id', 'item_type', 'author'])





 Give it a shot and let me know if it works!



0 Kudos