Python:将 JSON 字典值写入 JSON 文件
Python: Write JSON dictionary values to a JSON file
我正在尝试:
- 从 JSON 字典列表中加载数据
- 将每个字典中特定键的值写入文件
然而,当我尝试将密钥对转储到新的 .json 文件时,它只打印最后一个字典密钥对。任何人都知道如何遍历每个字典并附加密钥对?我尝试了几种方法,但我似乎无法弄清楚我缺少什么以及在哪里。
这是我的代码:
with open(join(dirname(__file__),'text.json')) as tone_json:
python_obj = json.load(tone_json) #read file object into string
my_list = python_obj["data"] #assign list name to string
for dictionary in my_list: #loop through dictionaries in list
for key,value in dictionary.items(): #loop through key pairs in dictionaries
if key == "text":
with open('comments.json', 'w') as f:
json.dump("{}: {}".format(key,value), f) #write key pair objects as json formatted stream to json file
f.write('\n')
我的 JSON 文件样本:
{
"data": [
{
"text": "apple",
"created_time": "2017-12-23",
"comment_count": 154,
"like_count": 856,
"id": "1015595299xxxxx"
},
{
"text": "orange",
"created_time": "2017-12-04",
"comment_count": 13,
"like_count": 437,
"id": "10155952xxxxx"
},
{
"text": "grapes",
"created_time": "2017-12-04",
"comment_count": 12,
"like_count": 163,
"id": "1015595299xxxxx"
}
]
}
我当前的输出:
"text: grapes"
但是,我想遍历每个字典并最终只打印每个 "text" 键的值。
预期输出:
"text: apple"
"text: orange"
"text: grapes"
任何提示都会有所帮助!谢谢!
您已在 w
模式下打开文件,您需要将其打开为 a
(追加模式)
来自 docs:
1. 'w' 仅用于写入(已存在的同名文件将被删除)
2。 'a' 打开文件进行追加;写入文件的任何数据都会自动添加到末尾
Python 3.6.5 (default, Mar 30 2018, 06:42:10)
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.39.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> my_list = [{u'created_time': u'2017-12-23', u'text': u'apple', u'comment_count': 154, u'like_count': 856, u'id': u'1015595299xxxxx'}, {u'created_time': u'2017-12-04', u'text': u'orange', u'comment_count': 13, u'like_count': 437, u'id': u'10155952xxxxx'}, {u'created_time': u'2017-12-04', u'text': u'grapes', u'comment_count': 12, u'like_count': 163, u'id': u'1015595299xxxxx'}]
>>> import json
>>> my_list = [{u'created_time': u'2017-12-23', u'text': u'apple', u'comment_count': 154, u'like_count': 856, u'id': u'1015595299xxxxx'}, {u'created_time': u'2017-12-04', u'text': u'orange', u'comment_count': 13, u'like_count': 437, u'id': u'10155952xxxxx'}, {u'created_time': u'2017-12-04', u'text': u'grapes', u'comment_count': 12, u'like_count': 163, u'id': u'1015595299xxxxx'}]
>>> for d in my_list:
... for key, value in d.items():
... if key == "text":
... with open('comments.json', 'a') as f: # Append mode here
... json.dump("{}: {}".format(key,value), f)
... f.write('\n')
...
comments.json
、
的内容
"text: apple"
"text: orange"
"text: grapes"
'r' open for reading (default)
'w' open for writing, truncating the file first
'x' open for exclusive creation, failing if the file already exists
'a' open for writing, appending to the end of the file if it exists
'b' binary mode
't' text mode (default)
'+' open a disk file for updating (reading and writing)
'U' universal newlines mode (deprecated)
如果我没有理解错的话,这应该可以满足您的要求:
with open('comments.json', 'a') as f:
json.dump("{}: {}".format(key,value), f) #write key pair objects as json formatted stream to json file
f.write('\n')
只需将 'w' 更改为 'a' 这样您就不会超过 w 仪式,而是 a 附加到文件
我正在尝试:
- 从 JSON 字典列表中加载数据
- 将每个字典中特定键的值写入文件
然而,当我尝试将密钥对转储到新的 .json 文件时,它只打印最后一个字典密钥对。任何人都知道如何遍历每个字典并附加密钥对?我尝试了几种方法,但我似乎无法弄清楚我缺少什么以及在哪里。
这是我的代码:
with open(join(dirname(__file__),'text.json')) as tone_json:
python_obj = json.load(tone_json) #read file object into string
my_list = python_obj["data"] #assign list name to string
for dictionary in my_list: #loop through dictionaries in list
for key,value in dictionary.items(): #loop through key pairs in dictionaries
if key == "text":
with open('comments.json', 'w') as f:
json.dump("{}: {}".format(key,value), f) #write key pair objects as json formatted stream to json file
f.write('\n')
我的 JSON 文件样本:
{
"data": [
{
"text": "apple",
"created_time": "2017-12-23",
"comment_count": 154,
"like_count": 856,
"id": "1015595299xxxxx"
},
{
"text": "orange",
"created_time": "2017-12-04",
"comment_count": 13,
"like_count": 437,
"id": "10155952xxxxx"
},
{
"text": "grapes",
"created_time": "2017-12-04",
"comment_count": 12,
"like_count": 163,
"id": "1015595299xxxxx"
}
]
}
我当前的输出:
"text: grapes"
但是,我想遍历每个字典并最终只打印每个 "text" 键的值。
预期输出:
"text: apple"
"text: orange"
"text: grapes"
任何提示都会有所帮助!谢谢!
您已在 w
模式下打开文件,您需要将其打开为 a
(追加模式)
来自 docs:
1. 'w' 仅用于写入(已存在的同名文件将被删除)
2。 'a' 打开文件进行追加;写入文件的任何数据都会自动添加到末尾
Python 3.6.5 (default, Mar 30 2018, 06:42:10)
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.39.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> my_list = [{u'created_time': u'2017-12-23', u'text': u'apple', u'comment_count': 154, u'like_count': 856, u'id': u'1015595299xxxxx'}, {u'created_time': u'2017-12-04', u'text': u'orange', u'comment_count': 13, u'like_count': 437, u'id': u'10155952xxxxx'}, {u'created_time': u'2017-12-04', u'text': u'grapes', u'comment_count': 12, u'like_count': 163, u'id': u'1015595299xxxxx'}]
>>> import json
>>> my_list = [{u'created_time': u'2017-12-23', u'text': u'apple', u'comment_count': 154, u'like_count': 856, u'id': u'1015595299xxxxx'}, {u'created_time': u'2017-12-04', u'text': u'orange', u'comment_count': 13, u'like_count': 437, u'id': u'10155952xxxxx'}, {u'created_time': u'2017-12-04', u'text': u'grapes', u'comment_count': 12, u'like_count': 163, u'id': u'1015595299xxxxx'}]
>>> for d in my_list:
... for key, value in d.items():
... if key == "text":
... with open('comments.json', 'a') as f: # Append mode here
... json.dump("{}: {}".format(key,value), f)
... f.write('\n')
...
comments.json
、
"text: apple"
"text: orange"
"text: grapes"
'r' open for reading (default)
'w' open for writing, truncating the file first
'x' open for exclusive creation, failing if the file already exists
'a' open for writing, appending to the end of the file if it exists
'b' binary mode
't' text mode (default)
'+' open a disk file for updating (reading and writing)
'U' universal newlines mode (deprecated)
如果我没有理解错的话,这应该可以满足您的要求:
with open('comments.json', 'a') as f:
json.dump("{}: {}".format(key,value), f) #write key pair objects as json formatted stream to json file
f.write('\n')
只需将 'w' 更改为 'a' 这样您就不会超过 w 仪式,而是 a 附加到文件