我习惯使用CSV文件将数据输入和输出Python,但这样做存在明显的挑战。是否有简单的方法可以将字典(或一组字典)存储在JSON或pickle文件中?
例如:
data = {}
data ['key1'] = "keyinfo"
data ['key2'] = "keyinfo2"
我想知道如何保存这个东西,然后如何重新加载它。
我习惯使用CSV文件将数据输入和输出Python,但这样做存在明显的挑战。是否有简单的方法可以将字典(或一组字典)存储在JSON或pickle文件中?
例如:
data = {}
data ['key1'] = "keyinfo"
data ['key2'] = "keyinfo2"
我想知道如何保存这个东西,然后如何重新加载它。
Pickle保存:
try:
import cPickle as pickle
except ImportError: # Python 3.x
import pickle
with open('data.p', 'wb') as fp:
pickle.dump(data, fp, protocol=pickle.HIGHEST_PROTOCOL)
有关protocol
参数的其他信息,请参见Pickle模块文档。
Pickle加载:
with open('data.p', 'rb') as fp:
data = pickle.load(fp)
JSON 保存:
import json
with open('data.json', 'w') as fp:
json.dump(data, fp)
可以提供额外参数,例如 sort_keys
或 indent
,以获得漂亮的结果。参数 sort_keys 将按字母顺序对键进行排序,indent 将使用indent=N
空格缩进数据结构。
json.dump(data, fp, sort_keys=True, indent=4)
JSON加载:
with open('data.json', 'r') as fp:
data = json.load(fp)
pickle.dump
的第三个参数也是值得的。如果文件不需要可读性,那么它可以大大加快速度。 - Steve Jessopdump
函数中添加sort_keys
和indent
参数,你将得到一个更美观的结果。例如:json.dump(data, fp, sort_keys=True, indent=4)
。更多信息可以在这里找到。 - juliusmhpickle.dump(data, fp, protocol=pickle.HIGHEST_PROTOCOL)
。 - Martin Thomaimport pickle
。 - Melroy van den Berg最简单的例子,直接将内容写入文件:
import json
json.dump(data, open(filename, 'wb'))
data = json.load(open(filename))
或者安全地打开/关闭:
import json
with open(filename, 'wb') as outfile:
json.dump(data, outfile)
with open(filename) as infile:
data = json.load(infile)
如果你想把它保存到字符串中而不是文件中:import json
json_str = json.dumps(data)
data = json.loads(json_str)
此外,还可以使用速度更快的包ujson:
import ujson
with open('data.json', 'wb') as fp:
ujson.dump(data, fp)
写入文件:
import json
myfile.write(json.dumps(mydict))
import json
mydict = json.loads(myfile.read())
< p > myfile
是您存储字典的文件的文件对象。
json.dump(myfile)
和 json.load(myfile)
- Niklas Rshelve
模块。可以将其视为持久字典。myData = shelve.open('/path/to/file')
# Check for values.
keyVar in myData
# Set values
myData[anotherKey] = someValue
# Save the data for future use.
myData.close()
json
更加方便。而 shelve
只适用于一次访问一个键的情况下更好用。 - agf>>> init = {'y': 2, 'x': 1, 'z': 3}
>>> import klepto
>>> cache = klepto.archives.file_archive('memo', init, serialized=False)
>>> cache
{'y': 2, 'x': 1, 'z': 3}
>>>
>>> # dump dictionary to the file 'memo.py'
>>> cache.dump()
>>>
>>> # import from 'memo.py'
>>> from memo import memo
>>> print memo
{'y': 2, 'x': 1, 'z': 3}
klepto
,如果你使用了 serialized=True
,字典将会以pickled字典的形式被写入到 memo.pkl
文件中,而不是以明文形式。klepto
: https://github.com/uqfoundation/klepto
dill
可能比 pickle
本身更好,因为 dill
可以序列化python中几乎任何内容。 klepto
也可以使用 dill
。dill
: https://github.com/uqfoundation/dill
前面几行的额外内容是因为 klepto
可以配置将字典存储到文件、目录上下文或SQL数据库中。无论你选择哪种后端归档,API都是相同的。它给你一个“可归档”字典,你可以使用 load
和 dump
方法与归档交互。为了完整性,我们应该包括ConfigParser和configparser,它们分别是Python 2和3的标准库的一部分。此模块读取和写入配置/ini文件,并且(至少在Python 3中)表现出许多字典的行为。它的附加好处是您可以将多个字典存储到配置/ini文件的不同部分中并进行调用。棒极了!
Python 2.7.x示例。
import ConfigParser
config = ConfigParser.ConfigParser()
dict1 = {'key1':'keyinfo', 'key2':'keyinfo2'}
dict2 = {'k1':'hot', 'k2':'cross', 'k3':'buns'}
dict3 = {'x':1, 'y':2, 'z':3}
# Make each dictionary a separate section in the configuration
config.add_section('dict1')
for key in dict1.keys():
config.set('dict1', key, dict1[key])
config.add_section('dict2')
for key in dict2.keys():
config.set('dict2', key, dict2[key])
config.add_section('dict3')
for key in dict3.keys():
config.set('dict3', key, dict3[key])
# Save the configuration to a file
f = open('config.ini', 'w')
config.write(f)
f.close()
# Read the configuration from a file
config2 = ConfigParser.ConfigParser()
config2.read('config.ini')
dictA = {}
for item in config2.items('dict1'):
dictA[item[0]] = item[1]
dictB = {}
for item in config2.items('dict2'):
dictB[item[0]] = item[1]
dictC = {}
for item in config2.items('dict3'):
dictC[item[0]] = item[1]
print(dictA)
print(dictB)
print(dictC)
Python 3.X示例。
import configparser
config = configparser.ConfigParser()
dict1 = {'key1':'keyinfo', 'key2':'keyinfo2'}
dict2 = {'k1':'hot', 'k2':'cross', 'k3':'buns'}
dict3 = {'x':1, 'y':2, 'z':3}
# Make each dictionary a separate section in the configuration
config['dict1'] = dict1
config['dict2'] = dict2
config['dict3'] = dict3
# Save the configuration to a file
f = open('config.ini', 'w')
config.write(f)
f.close()
# Read the configuration from a file
config2 = configparser.ConfigParser()
config2.read('config.ini')
# ConfigParser objects are a lot like dictionaries, but if you really
# want a dictionary you can ask it to convert a section to a dictionary
dictA = dict(config2['dict1'] )
dictB = dict(config2['dict2'] )
dictC = dict(config2['dict3'])
print(dictA)
print(dictB)
print(dictC)
{'key2': 'keyinfo2', 'key1': 'keyinfo'}
{'k1': 'hot', 'k2': 'cross', 'k3': 'buns'}
{'z': '3', 'y': '2', 'x': '1'}
[dict1]
key2 = keyinfo2
key1 = keyinfo
[dict2]
k1 = hot
k2 = cross
k3 = buns
[dict3]
z = 3
y = 2
x = 1
如果要保存到JSON文件中,最好、最简单的方法是:
import json
with open("file.json", "wb") as f:
f.write(json.dumps(dict).encode("utf-8"))
json.dump()
更容易? - baxx我的使用情况是将多个 JSON 对象保存到文件中,marty's answer 帮助了我一些。但是对于我的用例,答案不完整,因为它会在每次保存新条目时覆盖旧数据。
要在文件中保存多个条目,必须检查旧内容(即在写入之前读取)。一个典型的包含 JSON 数据的文件将具有根列表
或对象
。因此,我认为我的 JSON 文件始终具有对象列表
,每次添加数据时,我只需首先加载列表,将我的新数据附加到其中,并将其转储回可写实例的文件w
:
def saveJson(url,sc): # This function writes the two values to the file
newdata = {'url':url,'sc':sc}
json_path = "db/file.json"
old_list= []
with open(json_path) as myfile: # Read the contents first
old_list = json.load(myfile)
old_list.append(newdata)
with open(json_path,"w") as myfile: # Overwrite the whole content
json.dump(old_list, myfile, sort_keys=True, indent=4)
return "success"
[
{
"sc": "a11",
"url": "www.google.com"
},
{
"sc": "a12",
"url": "www.google.com"
},
{
"sc": "a13",
"url": "www.google.com"
}
]
注意:这种方法必须有一个名为file.json
的文件,并且其初始数据为[]
附:与原问题无关,但是这种方法也可以通过首先检查条目是否已存在(基于一个或多个键),然后只追加和保存数据来进一步改进。
更短的代码
只需一行代码即可保存和加载所有类型的Python变量(包括字典)。
data = {'key1': 'keyinfo', 'key2': 'keyinfo2'}
保存中:
pickle.dump(data, open('path/to/file/data.pickle', 'wb'))
加载中:
data_loaded = pickle.load(open('path/to/file/data.pickle', 'rb'))