我在文件中有这个JSON:
{
"maps": [
{
"id": "blabla",
"iscategorical": "0"
},
{
"id": "blabla",
"iscategorical": "0"
}
],
"masks": [
"id": "valore"
],
"om_points": "value",
"parameters": [
"id": "valore"
]
}
Run Code Online (Sandbox Code Playgroud)
我写了这个脚本打印所有的json文本:
import json
from pprint import pprint
with open('data.json') as f:
data = json.load(f)
pprint(data)
Run Code Online (Sandbox Code Playgroud)
如何解析文件并提取单个值?
我将JSON数据存储在变量中data.
我想将其写入文本文件进行测试,因此我不必每次都从服务器获取数据.
目前,我正在尝试这个:
obj = open('data.txt', 'wb')
obj.write(data)
obj.close
Run Code Online (Sandbox Code Playgroud)
我收到错误:
TypeError: must be string or buffer, not dict
如何解决这个问题?
我的项目当前正在python中收到一条JSON消息,我需要从中获取一些信息.出于此目的,我们将其设置为字符串中的一些简单JSON:
jsonStr = '{"one" : "1", "two" : "2", "three" : "3"}'
Run Code Online (Sandbox Code Playgroud)
到目前为止,我一直在使用列表生成JSON请求,json.dumps但是为了做到与此相反,我认为我需要使用它,json.loads但我没有太多运气.任何人都可以给我一个片段,在上面的例子中输入"2"会返回"2"吗?
我想通过Google Directions API动态查询Google地图.例如,此请求通过乔普林,密苏里州和俄克拉荷马城的两个航路点计算从伊利诺伊州芝加哥到洛杉矶的路线,OK:
它以JSON格式返回结果.
我怎么能用Python做到这一点?我想发送这样的请求,接收结果并解析它.
在Python中,json.load()和之间有什么区别json.loads()?
我想load()函数必须与文件对象一起使用(我需要使用上下文管理器),而loads()函数将文件的路径作为字符串.这有点令人困惑.
字母" s " 是否json.loads()代表字符串?
非常感谢你的回答!
从2gis API我得到以下JSON字符串.
{
"api_version": "1.3",
"response_code": "200",
"id": "3237490513229753",
"lon": "38.969916127827",
"lat": "45.069889625267",
"page_url": null,
"name": "ATB",
"firm_group": {
"id": "3237499103085728",
"count": "1"
},
"city_name": "Krasnodar",
"city_id": "3237585002430511",
"address": "Turgeneva, 172/1",
"create_time": "2008-07-22 10:02:04 07",
"modification_time": "2013-08-09 20:04:36 07",
"see_also": [
{
"id": "3237491513434577",
"lon": 38.973110606808,
"lat": 45.029031222211,
"name": "Advance",
"hash": "5698hn745A8IJ1H86177uvgn94521J3464he26763737242Cf6e654G62J0I7878e",
"ads": {
"sponsored_article": {
"title": "Center "ADVANCE"",
"text": "Business.English."
},
"warning": null
}
}
]
}
Run Code Online (Sandbox Code Playgroud)
但Python不承认它:
json.loads(firm_str)
Run Code Online (Sandbox Code Playgroud)
期待,分隔符:第1行第3646行(字符3645)
它看起来像引号中的问题:"标题":"中心"ADVANCE""
如何在Python中自动修复它?
我正在尝试创建一个程序,它将通过GUI读取JSON字符串,然后使用它来执行其他功能,在这种情况下,分解数学方程式.目前我收到错误:
"TypeError:字符串索引必须是整数"
而且我不知道为什么.
我想读的JSON如下:
{
"rightArgument":{
"cell":"C18",
"value":9.5,
"type":"cell"
},
"leftArgument":{
"rightArgument":{
"cell":"C3",
"value":135,
"type":"cell"
},
"leftArgument":{
"rightArgument":{
"cell":"C4",
"value":125,
"type":"cell"
},
"leftArgument":{
"cell":"C5",
"value":106,
"type":"cell"
},
"type":"operation",
"operator":"*"
},
"type":"operation",
"operator":"+"
},
"type":"operation",
"operator":"+"
}
Run Code Online (Sandbox Code Playgroud)
import json
import tkinter
from tkinter import *
data = ""
list = []
def readText():
mtext=""
mtext = strJson.get()
mlabel2 = Label(myGui,text=mtext).place(x=180,y=200)
data = mtext
def mhello():
_getCurrentOperator(data)
def _getCurrentOperator(data):
if data["type"] == "operation":
_getCurrentOperator(data["rightArgument"])
_getCurrentOperator(data["leftArgument"])
list.append(data["operator"])
elif data["type"] == "group":
_getCurrentOperator(data["argument"])
elif …Run Code Online (Sandbox Code Playgroud) 我想批量上传一个json文件到dynamodb。目前,我可以成功地手动将项目放入Python文件中(如下所示)并上传到表中,但是如何修改脚本以读取外部json文件(包含200个项目)并将所有200个项目批量上传到表中。
import boto3
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('exampletable')
with table.batch_writer() as batch:
batch.put_item(
Item={
'ID': '2',
'DateTime': '21/12/2017 13:16',
'SourceDevice': '10',
'DestinationDevice': '20',
'DataType': 'full',
'Activity': 'unusual'
}
)
batch.put_item(
Item={
'ID': '3',
'DateTime': '21/12/2017 13:40',
'SourceDevice': '10',
'DestinationDevice': '20',
'DataType': 'full',
'Activity': 'unusual'
}
)
Run Code Online (Sandbox Code Playgroud)
json文件内容如下
[{
"ID": "1",
"DateTime": "21/12/2017 13:16",
"SourceDevice": "10",
"DestinationDevice": "20",
"DataType": "part",
"Activity": "normal"
}, {
"ID": "1",
"DateTime": "21/12/2017 13:16",
"SourceDevice": "40",
"DestinationDevice": "25",
"DataType": "full",
"Activity": "unusual"
}]
Run Code Online (Sandbox Code Playgroud) 试图找到一种将数据从JSON文件导入Python的简单方法.我最初的想法是逐行读取文件,但这可能意味着应该在库中进行一些额外的处理.
理想的解决方案看起来像:
import json_library
the_data = json_library.load_from_file('my_file.json')
Run Code Online (Sandbox Code Playgroud)
其中'my_file.json'包含一个JSON格式的变量.
我有一个文件,其结构与python list/dictionaries相同,即
[
{
"key1" : "value1",
"key2" : "value2",
...
},
{
"key1" : "value3",
"key2" : "value4",
...
},
...
]
Run Code Online (Sandbox Code Playgroud)
有一些简单的方法如何读取此文件并将其转换为字典列表?
我有一个文本文件,其中包含以 UTF-8 格式存储的 json 数据,如下所示:
\n\n{\'name\': u\'\xd8\xa7\xd8\xad\xd8\xb3\xd8\xa7\xd9\x86\', \'family\': u\'\xd8\xb4\xdb\x8c\xd8\xb1\xd8\xb2\xd8\xa7\xd8\xaf\xdb\x8c\'}\nRun Code Online (Sandbox Code Playgroud)\n\n我尝试使用以下代码读取和打印文件数据:
\n\nfile = open("output.txt", "r")\ntext = file.read()\nfile.close()\nprint text\nRun Code Online (Sandbox Code Playgroud)\n\n没问题,和我在文件中看到的一模一样。但是当我尝试通过这样的索引打印字典的某些部分时:
\n\nfile = open("output.txt", "r")\ntext = file.read()\nfile.close()\nprint text[\'name\']\nRun Code Online (Sandbox Code Playgroud)\n\n一个错误说:
\n\n print text[\'name\']\nTypeError: string indices must be integers, not str\nRun Code Online (Sandbox Code Playgroud)\n\n但是当我直接运行这段代码时,我可以看到它正在工作:
\n\ntemp = {\'name\': u\'\xd8\xa7\xd8\xad\xd8\xb3\xd8\xa7\xd9\x86\', \'family\': u\'\xd8\xb4\xdb\x8c\xd8\xb1\xd8\xb2\xd8\xa7\xd8\xaf\xdb\x8c\'}\nprint temp[\'name\']\nRun Code Online (Sandbox Code Playgroud)\n\n这里有什么问题?
\n我试图用Python读取一个JSON文件,不管你信不信,我不知道从哪里开始.以下是我的JSON文件名称AEE_2007.json的样子:
{"date": "October 30, 2007 Tuesday", "body": "For those of us who have been around Aberdeen for a while, your question \"What now for the oil industry? (Evening Express, October 26) had a touch of deja vu about it. That same question has been asked almost since the day the first drop of oil was pumped out of the North Sea. In the past 30 years we have seen a constant cycle of ups and downs, booms and busts in …Run Code Online (Sandbox Code Playgroud) 我想用 Python 读取 JSON 文件:
这是我的 JSON 文件的一部分:
{ "Jointure":[ { "IDJointure":1, "societe":"S.R.T.K", "date":"2019/01/01", "heure":"05:47:00"}, { "IDJointure":2, "societe":"S.R.T.K", "date":"2019/01/01", "heure":"05:50:00"}]}
Run Code Online (Sandbox Code Playgroud)
这是代码:
import json
data = json.loads('Data2019.json')
for i in data['Jointure']:
print(i)
Run Code Online (Sandbox Code Playgroud)
但是,这是显示的错误
Traceback (most recent call last):
File "C:\Users\HP\Desktop\readJSON.py", line 4, in <module>
data = json.loads('Data2019.json')
File "C:\Users\HP\AppData\Local\Programs\Python\Python38\lib\json\__init__.py", line 357, in loads
return _default_decoder.decode(s)
File "C:\Users\HP\AppData\Local\Programs\Python\Python38\lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Users\HP\AppData\Local\Programs\Python\Python38\lib\json\decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: …Run Code Online (Sandbox Code Playgroud) python ×12
json ×11
dictionary ×2
file ×2
parsing ×2
boto3 ×1
escaping ×1
list ×1
python-2.7 ×1
python-3.x ×1
string ×1
typeerror ×1