I have written a small Python script which utilizes speedtest-cli
to get my internet speed every so often. I am planning to create a contraption where my Python script stores the speed data in a JSON array file that already exists on the disk and then have a small webpage setup to view the data.
我写了一个小的Python脚本,利用speedtest-cli来经常提高我的网速。我打算创建一个装置,我的Python脚本将速度数据存储在磁盘上已存在的JSON数组文件中,然后设置一个小网页来查看数据。
Anyways, I have configured my script in such a way that it spits out a dict
at the end of the script that looks like this:
无论如何,我已经配置了我的脚本,它在脚本末尾吐出一个dict,如下所示:
{
"downSpeed": 90.30834444821808,
"humanTime": "Fri Dec 9 18:02:54 2016",
"time": 1481328174225.999,
"upSpeed": 13.698571043448549
}
I have a JSON file on the disk that is simply a file with two brackets to represent a JSON array and within that array, I want my script to put the dicts
in the JSON array, separated by commas to represent the dicts
as objects
in JSON. My question is how would I go about entering my data into the JSON file? All the solutions that I have come across have mentioned using the open
function alongside the a
option. But, there are a couple of issues with this, first off, the data is being appended to the file and the last bracket of the JSON gets cut off because the data is being appended. Secondly, since I plan to run this script pretty frequently, I am afraid that I will run into an issue where the JSON file will be very long and therefore take a very long time for the script to process. So, what are my options here? Is there another way to insert a dict
into a JSON array (which I guess loosely translates into a list
in Python)?
我在磁盘上有一个JSON文件,它只是一个带有两个括号的文件来表示JSON数组,在该数组中,我希望我的脚本将dicts放在JSON数组中,用逗号分隔,以表示作为JSON中的对象的dicts 。我的问题是如何将数据输入JSON文件?我遇到的所有解决方案都提到了使用open函数和a选项。但是,这有几个问题,首先,数据被附加到文件,JSON的最后一个括号因为数据被追加而被切断。其次,由于我计划经常运行这个脚本,我担心我会遇到JSON文件很长的问题,因此需要很长时间才能处理脚本。那么,我的选择是什么?是否有另一种方法可以将dict插入JSON数组(我想这可以松散地转换为Python中的列表)?
3 个解决方案
#1
0
Are you familiar with the python JSON module? https://docs.python.org/2/library/json.html You can write a python object directly to a JSON file using dumps(). Example from the site:
你熟悉python JSON模块吗? https://docs.python.org/2/library/json.html您可以使用dumps()将python对象直接写入JSON文件。网站示例:
import json
print json.dumps({'4': 5, '6': 7}, sort_keys=True,
indent=4, separators=(',', ': '))
{
"4": 5,
"6": 7
}
#2
0
In general there is no easy way to insert something into the middle of a file. So you would have to parse all the JSON to create a list in memory, append an item, and then dump it again. Or you could mess around with commas and square brackets to manipulate the JSON array manually.
通常,没有简单的方法可以将某些内容插入文件的中间。因此,您必须解析所有JSON以在内存中创建列表,附加项目,然后再次转储它。或者你可以用逗号和方括号来手动操作JSON数组。
A much better option in this case it to forget the JSON array and just store each JSON object on a single line. This way you can use append mode and also easily iterate through the objects without loading the entire file into memory.
在这种情况下,更好的选择是忘记JSON数组并将每个JSON对象存储在一行中。这样,您可以使用追加模式,也可以轻松遍历对象,而无需将整个文件加载到内存中。
#3
0
If you would like to append JSON code to an already existing JSON file(.json) then all you need to use is Python's json module. Say you have a file named test.json
with code,
如果您想将JSON代码附加到现有的JSON文件(.json),那么您需要使用的只是Python的json模块。假设你有一个名为test.json的文件,带有代码,
[ "Hello World!", "Testing 1, 2, 3." ]
Then you want to append the following Python dictionary to the list.
然后,您要将以下Python字典附加到列表中。
{ "Hello World!": 123, "Test": None }
The python code would look like this.
python代码看起来像这样。
import json # import the module
code = json.load(open('test.json','r')) # load the current data
adding_dict = # put the python dictionary here,
# it can even be a class if it returns a dictionary.
new_dict = code.append(adding_dict) # append the dictionary to the list
# then we dump it to the file.
json.dump(new_dict, open('test.json', 'w'))
If you want more on this there is more on Python's documentation site, here.
如果您对此有更多了解,那么Python的文档站点上有更多内容。
#1
0
Are you familiar with the python JSON module? https://docs.python.org/2/library/json.html You can write a python object directly to a JSON file using dumps(). Example from the site:
你熟悉python JSON模块吗? https://docs.python.org/2/library/json.html您可以使用dumps()将python对象直接写入JSON文件。网站示例:
import json
print json.dumps({'4': 5, '6': 7}, sort_keys=True,
indent=4, separators=(',', ': '))
{
"4": 5,
"6": 7
}
#2
0
In general there is no easy way to insert something into the middle of a file. So you would have to parse all the JSON to create a list in memory, append an item, and then dump it again. Or you could mess around with commas and square brackets to manipulate the JSON array manually.
通常,没有简单的方法可以将某些内容插入文件的中间。因此,您必须解析所有JSON以在内存中创建列表,附加项目,然后再次转储它。或者你可以用逗号和方括号来手动操作JSON数组。
A much better option in this case it to forget the JSON array and just store each JSON object on a single line. This way you can use append mode and also easily iterate through the objects without loading the entire file into memory.
在这种情况下,更好的选择是忘记JSON数组并将每个JSON对象存储在一行中。这样,您可以使用追加模式,也可以轻松遍历对象,而无需将整个文件加载到内存中。
#3
0
If you would like to append JSON code to an already existing JSON file(.json) then all you need to use is Python's json module. Say you have a file named test.json
with code,
如果您想将JSON代码附加到现有的JSON文件(.json),那么您需要使用的只是Python的json模块。假设你有一个名为test.json的文件,带有代码,
[ "Hello World!", "Testing 1, 2, 3." ]
Then you want to append the following Python dictionary to the list.
然后,您要将以下Python字典附加到列表中。
{ "Hello World!": 123, "Test": None }
The python code would look like this.
python代码看起来像这样。
import json # import the module
code = json.load(open('test.json','r')) # load the current data
adding_dict = # put the python dictionary here,
# it can even be a class if it returns a dictionary.
new_dict = code.append(adding_dict) # append the dictionary to the list
# then we dump it to the file.
json.dump(new_dict, open('test.json', 'w'))
If you want more on this there is more on Python's documentation site, here.
如果您对此有更多了解,那么Python的文档站点上有更多内容。