Skip to content
Advertisement

Use a python list in a Socrata Request

I have connected to a public dataset using Socrata. As this dataset is very large, I would like to use a where statement in the .get statement to filter out certain records.

I only want to import records for which I have matching records in my SQL database. I have created a Python list of these Id’s.

I would like to run something like below, but this syntax is invalid.

result = client.get(socrata_dataset_id , where ='id in id_list')

Is it possible to use a python list to as a filter in the client.get statement or is there a better way to do this?

Advertisement

Answer

where expects a string. You could do something like below, it’s not very optimized but you get the idea.

ids = [1, 2, 3, 4]
ids_str = "id in ("
for i in ids:
    ids_str += f"'{str(i)}',"
ids_str = ids_str[:-1]
ids_str += ")"
print(ids_str)

result = client.get(socrata_dataset_id , where = ids_str)

Prints

id in ('1','2','3','4')

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement