Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 无效语法的未知原因_Python_Api_Weather - Fatal编程技术网

Python 无效语法的未知原因

Python 无效语法的未知原因,python,api,weather,Python,Api,Weather,我不明白为什么会收到无效语法错误。我做了一个小时的研究,但没有成功。我正在运行PYTHON 3。这段代码中的语法错误在哪里 from urllib.request import urlopen import json request = urlopen("http://api.aerisapi.com/observations/Urbandale,IA?client_id=QD2ToJ2o7MKAX47vrBcsC&client_secret=0968kxX4DWybMkA

我不明白为什么会收到无效语法错误。我做了一个小时的研究,但没有成功。我正在运行PYTHON 3。这段代码中的语法错误在哪里

  from urllib.request import urlopen
  import json

  request = urlopen("http://api.aerisapi.com/observations/Urbandale,IA?client_id=QD2ToJ2o7MKAX47vrBcsC&client_secret=0968kxX4DWybMkA9GksQREwBlBlC4njZw9jQNqdO")
  response = request.read().decode("utf-8")
  json = json.loads(response)
  if json['success']:
      ob = json['respnose']['ob']
      print ("the current weather in urbandale is %s with a temperature of %d") % (ob['weather'].lower(), ob['tempF']
 else
      print "An error occurred: %s" % (json['error']['description'])
 request().close 
线路

print ("the current weather in urbandale is %s with a temperature of %d") % (ob['weather'].lower(), ob['tempF']
他错过了一个闭幕式

应该是

print ("the current weather in urbandale is %s with a temperature of %d" % (ob['weather'].lower(), ob['tempF']))
Python可能会在下一行抱怨它

您需要一个:在else之后

此行上的和的数量不相等:

>>> strs = """print ("the current weather in urbandale is %s with a temperature of %d") % (ob['weather'].lower(), ob['tempF']"""
>>> strs.count('(')
3
>>> strs.count(')')
2
如果else应该像这样正确缩进:

if json['success']:
    ob = json['respnose']['ob']
    print ("the current weather in urbandale is %s with a temperature of %d") % (ob['weather'].lower(), ob['tempF'])
else:
    print "An error occurred: %s" % (json['error']['description'])
有几个原因:

括号不平衡:

print ("the current weather in urbandale is %s with a temperature of %d") % (ob['weather'].lower(), ob['tempF']
缺少一个右括号,而您的右括号位置错误

这应该是:

print ("the current weather in urbandale is %s with a temperature of %d" % (ob['weather'].lower(), ob['tempF']))
您的else语句缺少:冒号

第二个print函数不是函数,而是一个Python 2语句。通过添加括号进行更正:

print("An error occurred: %s" % (json['error']['description']))
您的缩进似乎不正确,但这可能是发布错误

您的最后一行也无效;您要呼叫关闭,而不是请求:

使用urllib,实际上不需要关闭对象

您拼错了respnose:

工作代码:

from urllib.request import urlopen
import json

request = urlopen("http://api.aerisapi.com/observations/Urbandale,IA?client_id=QD2ToJ2o7MKAX47vrBcsC&client_secret=0968kxX4DWybMkA9GksQREwBlBlC4njZw9jQNqdO")
response = request.read().decode("utf-8")
json = json.loads(response)
if json['success']:
    ob = json['response']['ob']
    print("the current weather in urbandale is %s with a temperature of %d" % (ob['weather'].lower(), ob['tempF']))
else:
    print("An error occurred: %s" % (json['error']['description']))

@apples723:这是因为Python不知道右括号何时会出现,只有在启动else:line时才会忽略它。好的,我不再得到语法错误,只有这一回最新调用最后:文件C:/Python33/sddf.py,第8行,在ob=json['respnose']['ob']关键错误:'respnose'@apples723:明白我的观点6.天哪,你的生活品味我从星期天开始一直在尝试写一个工作脚本现在是星期四谢谢谢谢你谢谢你,你至少读过了吗?我用维基书籍,但不是维基百科这是我在学校学到的不要用wilikpedia,我使用官方文档和邮件列表,这就是我从职业生涯中学到的但即使是wikipedia的知识也足以解决这个问题:这个问题甚至不包括遇到的错误消息。它有入侵的语法,这是唯一的错误没有跟踪错误好的,我更改了它并得到了这个回拨最近的调用最后:文件C:/Python33/sddf.py,第8行,在ob=json['response'['ob']KeyError:“respnose”
request.close()
ob = json['response']['ob']
from urllib.request import urlopen
import json

request = urlopen("http://api.aerisapi.com/observations/Urbandale,IA?client_id=QD2ToJ2o7MKAX47vrBcsC&client_secret=0968kxX4DWybMkA9GksQREwBlBlC4njZw9jQNqdO")
response = request.read().decode("utf-8")
json = json.loads(response)
if json['success']:
    ob = json['response']['ob']
    print("the current weather in urbandale is %s with a temperature of %d" % (ob['weather'].lower(), ob['tempF']))
else:
    print("An error occurred: %s" % (json['error']['description']))