Home > Enterprise >  Write dictionary content to files with keys as filename
Write dictionary content to files with keys as filename

Time:12-06

I have a dictionary in an object called "month_data", that contains lists as values.

    {
     '2000-01': [
      {
        'lat': 52.54,
        'lon': 13.54,
        'timestamp': '2000-01-01 01:00:00',
        'temp': -0.57,
        'feels_like': -2.64,
        'humidity': 97,
        'pressure': 1024
    },
    {
        'lat': 52.54,
        'lon': 13.54,
        'timestamp': '2000-01-01 02:00:00',
        'temp': -0.38,
        'feels_like': -2.46,
        'humidity': 97,
        'pressure': 1024
      }
    ], 
    '2000-02': [
    {
        'lat': 52.54,
        'lon': 13.54,
        'timestamp': '2000-01-01 01:00:00',
        'temp': -0.57,
        'feels_like': -2.64,
        'humidity': 97,
        'pressure': 1024
    },
    {
        'lat': 52.54,
        'lon': 13.54,
        'timestamp': '2000-01-01 02:00:00',
        'temp': -0.38,
        'feels_like': -2.46,
        'humidity': 97,
        'pressure': 1024
      }
    ]
  } 

I would like to write the content into separate files in the s3 bucket but it fails so far. The error is:

ParamValidationError: Parameter validation failed: Here is the code:

for month, data in month_data.items():
    s3.put_object(Bucket=s3_bucket, Body=data ,Key=f"{month}.json")

Has anyone an idea why it fails?

Thank you

A

CodePudding user response:

It looks like you are trying to save the data as JSON so you should write a JSON string using json.dumps, as follows:

import boto3
import json

s3 = boto3.client("s3")

s3_bucket = "mybucket"
month_data = { ... }

for month, data in month_data.items():
    s3.put_object(
        Bucket=s3_bucket, Body=json.dumps(data), Key=f"{month}.json"
    )
  • Related