我的 JSON 看起來像這樣(但有很多這樣的行):
{"text": "Home - Homepage des Kunstvereins Pro Ars Lausitz e.V.\nKunst. Und so weiter.", "timestamp": "2018-01-20T18:56:35Z", "url": "http://proarslausitz.de/1.html"}
{"text": "Bildnummer: 79800031\nVektorgrafikSkalieren Sie ohne Aufl\u00f6sungsverlust auf jede beliebige. Ende.", "url": "http://www.shutterstock.com/de/pic.mhtml?id=79800031&src=lznayUu4-IHg9bkDAflIhg-1-15"}
我想創建一個.txt僅包含text. 所以它只是:
Home - Homepage des Kunstvereins Pro Ars Lausitz e.V.\nKunst. Und so weiter. Bildnummer: 79800031\nVektorgrafikSkalieren Sie ohne Aufl\u00f6sungsverlust auf jede beliebige. Ende.
沒有字串,什么都沒有。編碼(因為變音)我認為事后不難解決。但關于文本提取,我知道我可以做到:
json_object = json.loads(json_object_string)
print(json_object["text"])
但這只是為了一條線。我需要遍歷這些行嗎?如何將文本合并到一個.txt檔案中?
uj5u.com熱心網友回復:
with open("file.txt", 'w') as txt_file:
for i in range(len(js_file['...'])):
txt_file.write(js['...'][i]['text'])
txt_file.close()
將 '...' 替換為 json 檔案的主鍵的名稱
uj5u.com熱心網友回復:
我不完全確定有一種方法可以“矢量化”從 json 復制值,即使有,在我看來,迭代仍然可以很好地完成作業。如果我要遍歷那個長 JSON 的每一行并將每個“文本”放入一個文本檔案中,我會這樣做:
import json
# removed escape sequences, that is not focus of problem
test = '[{"text": "Home - Homepage des Kunstvereins Pro Ars Lausitz e.V.Kunst. Und so weiter.", "timestamp": "2018-01-20T18:56:35Z", "url": "http://proarslausitz.de/1.html"}, {"text": "Bildnummer: 79800031VektorgrafikSkalieren Sie ohne Aufl sungsverlust auf jede beliebige. Ende.", "url": "http://www.shutterstock.com/de/pic.mhtml?id=79800031&src=lznayUu4-IHg9bkDAflIhg-1-15"}]'
# as you said loading the object from list of dicts into json
test_json = json.loads(test)
# opens a new text file to put the json text into
with open("json_output.txt", 'w ') as file:
for line in test_json:
# assuming the text includes /n write function will paste each dict on different line
file.write(line.get("text"))
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/368888.html
上一篇:嘗試根據過濾器洗掉Pandas資料框中的行時出現關鍵錯誤
下一篇:帶條件鍵的Python字典
