python - 读取和操作 Sagemaker Json 输出
问题描述
我将 HuggingFace Transformer 模型作为批处理部署在 Sagemaker 上。我的输出文件是一个 .jsonl.out 文件,如下所示:
{"SageMakerOutput":[{"label":"LABEL_8","score":0.9152628183364868}],"inputs":"test"}
{"SageMakerOutput":[{"label":"LABEL_8","score":0.9769203066825867}],"inputs":"Alles OK"}
现在的问题是我只想要以下输出:
LABEL_8, test
LABEL_8, Alles OK
并将其作为 .csv 或 .xlsx 返回!我尝试过类似的东西:
batch_transform_result = []
with open(output_file) as f:
for line in f:
# converts jsonline array to normal array
line = "[" + line.replace("[","").replace("]","") + "]"
batch_transform_result = literal_eval(line)
并尝试添加更多 .replace() 函数来清理文本中的读取内容,但没有奏效。有什么建议么?
解决方案
我认为以下内容可以为您工作
lst = [
{"SageMakerOutput": [{"label": "LABEL_8", "score": 0.9152628183364868}], "inputs": "test"},
{"SageMakerOutput": [{"label": "LABEL_8", "score": 0.9769203066825867}], "inputs": "Alles OK"}
]
result = [(entry['SageMakerOutput'][0]['label'],entry['inputs']) for entry in lst]
print(result)
输出
[('LABEL_8', 'test'), ('LABEL_8', 'Alles OK')]
推荐阅读
- refresh - 数据“过期”并且不会在 Tableau Server 中刷新
- r - 使用邻接向量为 R 矩阵或数据框赋值(特别是使用行名和列名)
- r - XLConnect saveWorkbook 功能无法将工作簿对象写入磁盘
- ios - 为 Facebook App Review 构建 React Native iOS 应用
- c# - 关于 MongoDB 中查询的疑问
- python - Python 正则表达式:两个分隔符之间的单词 - 用标点符号替换前导分隔符,但删除结尾分隔符
- python - 检索活动目录数据,按特定组名过滤
- javascript - 带有路由器的 React-Redux 未更新 componentDidMount 中的状态
- python - python sqlite - 将相同的值分配给多个?UPSERT 中的参数替换
- arm - 交叉编译中的 libtool:仅在 arm 目标上重新链接问题;i386/x86_64/aarch64 目标没问题