Analyzing and Publishing CSV to Portal

751
1
05-05-2022 09:57 AM
moremeowbell
New Contributor III

I am in the process of creating a python script using the ArcGIS API for Python. My goal is to intake a CSV file, analyze it, and publish it. When publishing it, ESRI documentation states that you should be able to take those analyzed parameters (looks like it returns a dictionary) and use those to publish the data. Could someone help me debug this script? When it run it, I get a "job failed" error message, but no description on why it failed.

#import modules
from arcgis.gis import GIS  # conda install arcgis -c esri
import os
from pathlib import Path

#create local variables
gis = GIS("home", verify_cert = False)
file = "E:/API_creation/upload/TestUpload.csv"
filename = Path(file).stem
print(filename)

try:
    #add content named "file"
    csv = gis.content.add(
        item_properties = {
            "type": "CSV", 
            "title": filename,
            "description": "Just a random Study Area",
            "tags": "Clip Area, Study Area",
        },
        data = file,
    )
    print(file + " uploaded successfully")
    
    #analyze CSV and get attribute dictionary
    csv_dict = gis.content.analyze (
        item = csv,
        file_type = "csv",
    )

    print(file + " analyzed successfully")
    print(csv_analyzed)

    #publish item named "file" as a feature layer
    #this needs to be built out so that it enables locations for csvs with lat/long/x/y
    spreadsheet_pub = csv.publish(
        
        csv_dict,
    )

    print(file + " published successfully")

except Exception as e:
    print(e)
0 Kudos
1 Reply
TonyContreras_Frisco_TX
Occasional Contributor III

The entire result of the analyze function is not intended to be used as the input for the publish function. There is a publishParameters subobject within the output that should be used.

ArcGIS Python API 

Something like this might work-

spreadsheet_pub = csv.publish(csv_dict['publishParameters'])

It might require using a json library to get access, but if it still doesn't work, print the csv_dict and compare it to the required parameters for publishing CSV files to see if anything doesn't match up.

0 Kudos