1

I am opening a CSV file in Python in Pycharm, then I want to upload it to my Container in Cosmos DB. It's not working.

if os.path.exists(csv_file):
    with codecs.open(csv_file, 'rb', encoding="utf-8") as csv:
        csv_reader = DictReader(csv)
        for row in csv_reader:
            upsert_item(row)

Can I just open a CSV file, then upload it to CosmosDB like the above and below? It's not working.

def upsert_item(row):
     container.upsert_item(row)

1 Answers1

0

It's failing because Cosmos DB expects the data contained in your row variable to be in JSON format.

If you're just uploading a small amount of data in CSV format, the easiest way is to use the Data Migration Tool.

You learn more about this tool and download it from here, Tutorial: Use Data migration tool to migrate your data to Azure Cosmos DB

Mark Brown
  • 8,113
  • 2
  • 17
  • 21
  • That seems be for .NET, I'm looking for a Python solution, it will be a pretty small amount so I think that solution would work. Can you suggest a way to do it with Python? https://pypi.org/project/azure-mgmt-datamigration/ , does that work? – Maria Evans Nov 04 '21 at 07:25
  • I'm not familiar with that data migration library. Looking at it, it appears to be SQL Server focused. If you have to do this in python then you need to take your data and write it into a JSON format. Here's a sample that may help you. https://github.com/Azure/azure-cosmos-python/blob/master/samples/DocumentManagement/Program.py#L95 – Mark Brown Nov 04 '21 at 16:10