我有一个小的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的各种组合,但找不到合适的组合。谢谢
3条答案
按热度按时间hjqgdpho1#
看起来您已经使用
json.dumps()
在正文中序列化了JSON响应可以使用函数
json.loads()
来反序列化JSON。试试看:
bvjveswy2#
exdqitrt3#
我接受了你的回答,并修改如下,以达到我所需要的。谢谢