正确格式化从S3存储桶中提取的csv的json响应

iklwldmw  于 2023-09-28  发布在  其他
关注(0)|答案(3)|浏览(92)

我有一个小的CSV,数据看起来像这样:

Ticker    Exchange    Date    Open    High    Low     Close   Volume
  6A        BATS    12/2/2021   0.9     0.95    0.83    0.95    1200
  6B        BATS    12/3/2021   1       1.3     0.9     1.2     1500
  6C        BATS    12/4/2021   1.2     1.3     1.1     1.1     1300

我使用下面的代码从bucket中提取最新的文件并打印为json。

import json
import pandas as pd
import boto3

s3 = boto3.client('s3')
object_list = []
bucket_name = 'bats-candles'

paginator = s3.get_paginator("list_objects_v2")
page_iterator = paginator.paginate(Bucket=bucket_name)
for result in page_iterator:
    object_list += filter(lambda obj: obj['Key'].endswith('.csv'), result['Contents'])
object_list.sort(key=lambda x: x['LastModified'])

A = (object_list[-1]['Key'])
full_path = f"s3://{bucket_name}/{A}"
print(full_path)
print(A)

raw_df = pd.read_csv(full_path)

print(bucket_name, full_path)

df = raw_df.to_json(orient="records")

response = {
    'body': json.dumps(df, indent=4, separators=(',', ':'))
}

pretty_json = json.dumps(response)
print(pretty_json)

我得到的数据看起来像这样:

{"body": "\"[{\\\"Ticker\\\":\\\"6A\\\",\\\"Exchange\\\":\\\"BATS\\\",\\\"Date\\\":\\\"12\\\\/2\\\\/2021\\\",\\\"Open\\\":0.9,\\\"High\\\":0.95,\\\"Low\\\":0.83,\\\"Close\\\":0.95,\\\"Volume\\\":1200}

但我希望它看起来像这样

[
        {
            "Ticker":"6A",
            "Exchange":"BATS",
            "Date":"12/2/2021",
            "Open":0.9,
            "High":0.95,
            "Low":0.83,
            "Close":0.95,
            "Volume":1200
        },
        {
            "Ticker":"6B",
            "Exchange":"BATS",
            "Date":"12/3/2021",
            "Open":1.0,
            "High":1.3,
            "Low":0.9,
            "Close":1.2,
            "Volume":1500
        },
        {
            "Ticker":"6C",
            "Exchange":"BATS",
            "Date":"12/4/2021",
            "Open":1.2,
            "High":1.3,
            "Low":1.1,
            "Close":1.1,
            "Volume":1300
        } ]

有谁能提出正确的方法来实现这一点吗?我已经尝试了json.dumps和json.loads的各种组合,但找不到合适的组合。谢谢

hjqgdpho

hjqgdpho1#

看起来您已经使用json.dumps()在正文中序列化了JSON响应
可以使用函数json.loads()来反序列化JSON。
试试看:

json.loads(pretty_json);
bvjveswy

bvjveswy2#

import pprint
s=''' Ticker    Exchange    Date    Open    High    Low     Close   Volume
  6A        BATS    12/2/2021   0.9     0.95    0.83    0.95    1200
  6B        BATS    12/3/2021   1       1.3     0.9     1.2     1500
  6C        BATS    12/4/2021   1.2     1.3     1.1     1.1     1300'''

z=s.split('\n')
res=[]
d={}
zz=z[0].split()
for st in range(1,len(z)):
    for t in range(len(z[st].split())):
         d[zz[t]]=z[st].split()[t]
    res.append(d)
pprint.pprint(res,sort_dicts=False)
[{'Ticker': '6C',
  'Exchange': 'BATS',
  'Date': '12/4/2021',
  'Open': '1.2',
  'High': '1.3',
  'Low': '1.1',
  'Close': '1.1',
  'Volume': '1300'},
 {'Ticker': '6C',
  'Exchange': 'BATS',
  'Date': '12/4/2021',
  'Open': '1.2',
  'High': '1.3',
  'Low': '1.1',
  'Close': '1.1',
  'Volume': '1300'},
 {'Ticker': '6C',
  'Exchange': 'BATS',
  'Date': '12/4/2021',
  'Open': '1.2',
  'High': '1.3',
  'Low': '1.1',
  'Close': '1.1',
  'Volume': '1300'}]
exdqitrt

exdqitrt3#

我接受了你的回答,并修改如下,以达到我所需要的。谢谢

raw_df = pd.read_csv(full_path)

df = raw_df.to_json(orient="records")
parsed = json.loads(df)
parsed_w_separators = json.dumps(parsed, indent=4, separators=(',', ': '))
print(parsed_w_separators)

相关问题