[英]How do I print a dictionary in python? (Prints it one by one on a separate line)
[英]Python, json, appending to one line dictionary {}{}{}{}{}, how do i read them one by one?
我正在嘗試創建一些簡單的 cfg 文件
import json
#write "function"
exDict = {}
data = {'A': 'a', 'B': 'b', }
exDict = {"yellow": 'apple', "square": 'ball'}
#Corrected typo
with open('CONFIG', 'w') as file:
file.write(json.dumps(exDict))
file.write(json.dump(data))
file.write(json.dumps(exDict))
file.write(json.dump(data))
讀取功能托盤:
附加后打開並讀取文件:
f = open("CONFIG", "r")
print(f.read())
Out
{"yellow": "apple", "square": "ball"}{"A": "a", "B": "b"}{"yellow": "apple", "square": "ball"}{"A": "a", "B": "b"}
現在該文件包含:
{"yellow": "apple", "square": "ball"}{"A": "a", "B": "b"}{"yellow": "apple", "square": "ball"}{"A": "a", "B": "b"}
#dont work if i try to
with open("CONFIG", "r") as CFG:
result = json.loads("CFG")
print(result)
#error code:json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
在同一行“{}{}{}{}”上,當我嘗試提取所有這些時,Ide 會拋出錯誤 dha...
json.decoder.JSONDecodeError: Expecting value: line "X" column "X" (char "X")
我讀了一大堆“過去 3 小時的搜索”,但沒有找到這個特殊案例,而是一大堆“逐行解析”,但我只有一行,我想保持這樣,這樣我就可以了解為什么結束了,所以我的問題是:
我如何同時提取一本字典?
我如何將它們全部提取在一起?
有一種方法可以提取 2-3 字典,特別是不提取所有內容?
您共享的代碼存在一些問題:
您正在寫入的文件稱為
CONFG
(缺少大寫“i”),而您正在讀取的文件稱為
CONFIG
。
我假設這只是一個錯字,而不是您的實際代碼。
固定的
當您將dict
寫入文件時,您同時使用json.dump()
和json.dumps()
。 使用json.dump()
時,您需要提供文件 object,所以我假設這是此處代碼中的拼寫錯誤。
你一次寫一個dict
。 這樣file.write()
只會將 append 寫入文件,這將導致一系列 JSON 對象在一行中。 這本身並沒有錯,它只是不符合實際的 JSON 標准,因此標准的 JSON 庫將無法處理它,這可能不是您想要的。 如果你想讓它工作,你可以考慮每行寫一個 JSON object (有時稱為JSON Lines )。
線
result = json.loads("CFG")
給你錯誤,因為你傳遞的是字符串"CFG"
而不是文件 object CFG
,即使你確實傳遞了文件 object,它也不會工作,因為你需要使用json.load()
s
)並且因為您正在將 JSON 對象寫入一行(根據 3.)。
有了以上所有,有不同的方法可以使這項工作:
第一個建議是將每個dict
寫為 output 文件中的每行一個 JSON object 文件,例如:
import json
data = {'A': 'a', 'B': 'b', }
exDict = {'yellow': 'apple', 'square': 'ball'}
# Write the dicts to a file, one JSON object per line:
with open('CONFIG.jsonl', 'w') as file:
file.write(json.dumps(exDict))
file.write('\n')
file.write(json.dumps(data))
file.write('\n')
file.write(json.dumps(exDict))
file.write('\n')
file.write(json.dumps(data))
file.write('\n')
# CONFIG.jsonl now contains:
#{"yellow": "apple", "square": "ball"}
#{"A": "a", "B": "b"}
#{"yellow": "apple", "square": "ball"}
#{"A": "a", "B": "b"}
# Read the file and parse one JSON line at a time:
dicts_from_json_lines = []
with open('CONFIG.jsonl', 'r') as CFG:
for line in CFG.readlines():
dicts_from_json_lines.append(json.loads(line))
另一種方法是將dict
存儲在list
並將該list
作為 JSON 寫入文件中,例如:
import json
data = {'A': 'a', 'B': 'b', }
exDict = {'yellow': 'apple', 'square': 'ball'}
list_of_dicts = [data, exDict]
# Write the list of dicts to a file:
with open('CONFIG.json', 'w') as file:
file.write(json.dumps(list_of_dicts))
# CONFIG.json now contains:
# [{"yellow": "apple", "square": "ball"}, {"A": "a", "B": "b"}]
# Read the file:
with open('CONFIG.json', 'r') as CFG:
dicts_from_json = json.load(CFG)
# or alternatively:
# dicts_from_json = json.loads(CFG.read())
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.