亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何將 AWS Athena 的多記錄多行 JSON 轉換為每記錄單行 JSON?

如何將 AWS Athena 的多記錄多行 JSON 轉換為每記錄單行 JSON?

BIG陽 2023-09-05 20:24:58
我想將 json 文件與 AWS Athena 結合使用,但 Athena 不支持多行 JSON。我有以下內容(其中一個值是 XML){  "id" : 10,  "name" : "bob",  "data" : "<some> \n <xml> \n <in here>"},{  "id" : 20,  "name" : "jane",  "data" : "<other> \n <xml> \n <in here>"}我需要雅典娜以下內容{ "id" : 10, "name" : "bob", "data" : "<some> <xml> <in here>" },{ "id" : 20, "name" : "jane", "data" : "<other> <xml> <in here>" }我正在使用 RazorSQL 從 DB2 導出數據,并嘗試使用 Python 編寫一些代碼來“扁平化”它,但尚未成功。謝謝你!
查看完整描述

3 回答

?
慕尼黑8549860

TA貢獻1818條經驗 獲得超11個贊

我最終做了一些快速而骯臟的事情


import json

with open('data.json') as jfile:

    data = json.load(jfile)

    for d in data:

        print(json.dumps(d) + ',')

哪個打印


{'id': 200, 'name': 'bob', 'data': '<other> \n <xml> \n <data>'},

{"id": 200, "name": "bob", "data": "<other> \n <xml> \n <data>"},

剛剛將輸出保存到另一個文件:P


結果失敗了,因為文件太大了,但是嘿..已經很接近了!


查看完整回答
反對 回復 2023-09-05
?
LEATH

TA貢獻1936條經驗 獲得超7個贊

使用正則表達式


import re

html = '''

{

  "id" : 10,

  "name" : "bob",

  "data" : "<some> \n <xml> \n <in here>"

},

{

  "id" : 20,

  "name" : "jane",

  "data" : "<other> \n <xml> \n <in here>"

}

'''



def replaceReg(html, regex, new):

    return re.sub(re.compile(regex), new, html)


html = replaceReg(html,' \n ',' ')

html = replaceReg(html,'{[\s]+','{ ')

html = replaceReg(html,'[\s]+}',' }')

html = replaceReg(html,',[\s]+',', ')

html = replaceReg(html,'}, ','\n')

print (html)

結果:


{ "id" : 10, "name" : "bob", "data" : "<some> <xml> <in here>" 

{ "id" : 20, "name" : "jane", "data" : "<other> <xml> <in here>" }


查看完整回答
反對 回復 2023-09-05
?
HUH函數

TA貢獻1836條經驗 獲得超4個贊

您只需要在寫入另一個文件時替換結束換行符(\n ):


s=''

with open('input.txt','r') as f_in, open('output.txt', 'w') as f_out:

    for line in f_in:        

        s += line.replace('\n', '')

    f_out.write(s)

其中 input.txt 具有以下數據:


{

  "id" : 10,

  "name" : "bob",

  "data" : "<some> \n <xml> \n <in here>"

},

{

  "id" : 20,

  "name" : "jane",

  "data" : "<other> \n <xml> \n <in here>"

}


查看完整回答
反對 回復 2023-09-05
  • 3 回答
  • 0 關注
  • 228 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號