Python SyntaxError: 无效语法 (<string>)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22443725/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 00:57:35  来源:igfitidea点击:

SyntaxError: invalid syntax (<string>)

pythonpython-2.7postgresql-9.1postgisplpython

提问by Balazs

I have python 2.7 and I have weather script which returns temperature infos, I would like to implement this script into PostgreSQL. I always got this error: DETAIL: SyntaxError: invalid syntax (<string>, line 10)

我有 python 2.7,我有返回温度信息的天气脚本,我想将此脚本实现到 PostgreSQL 中。我总是收到这个错误:DETAIL: SyntaxError: invalid syntax (<string>, line 10)

code:

代码:

    CREATE OR REPLACE FUNCTION GetWeather(lon float, lat float)
    RETURNS float
    AS $$
    import urllib2
    import simplejson as json

    data = urllib2.urlopen(
    "http://api.openweathermap.org/data/2.1/find/station?lat=%s&lon=%s&cnt=1"% (lat, lon))
    js_data = json.load(data)
    if js_data['cod'] == '200': 
    if int(js_data['cnt'])>0: 
    station = js_data['list'][0] 
    print 'Data from weather station %s' %station['name']
    if 'main' in station: 
    if 'temp' in station['main']: 
    temperature = station['main']['temp'] - 273.15 
    else:temperature = None
    else:temperature = None

    return temperature

    $$ LANGUAGE plpythonu;

I tried this version too, but its not working

我也试过这个版本,但是不行

CREATE OR REPLACE FUNCTION GetWeather(lon float, lat float)
    RETURNS float
    AS $$
    import urllib2
    import simplejson as json

    def get_temp(lat, lon):
    data = urllib2.urlopen(
    "http://api.openweathermap.org/data/2.1/find/station?lat=%s&lon=%s&cnt=1"% (lat, lon))
    js_data = json.load(data)
    try:
    return js_data['list'][0]['main']['temp']
    except (KeyError, IndexError):
        return None

    $$ LANGUAGE plpythonu;

采纳答案by Mike T

I only have plpython3u, but it should also work for Python 2.7 using plpythonu(just change that part of the following and nothing else).

我只有plpython3u,但它也应该适用于 Python 2.7 使用plpythonu(只需更改以下的那部分即可)。

CREATE OR REPLACE FUNCTION GetWeather(lon float, lat float)
  RETURNS float AS $$
import sys
if sys.version_info[0] == 2:
    from urllib2 import urlopen
else:  # Python 3
    from urllib.request import urlopen
import json

def get_temp(lon, lat):
    data = urlopen(
        "http://api.openweathermap.org/data/2.1/find/station?lat=%s&lon=%s&cnt=1"
        % (lat, lon))
    js_data = json.loads(data.read().decode('utf-8'))
    try:
        return js_data['list'][0]['main']['temp']
    except (KeyError, IndexError):
        return None

return get_temp(lon, lat)
$$ LANGUAGE plpython3u;

Note that the above is with a 4-space indent convention (PEP 8). If you are new to Python, I'd recommend going through a few tutorials to understand the syntax and use of indents.

请注意,以上是4 空格缩进约定 (PEP 8)。如果您不熟悉 Python,我建议您阅读一些教程以了解缩进的语法和使用。

回答by m.wasowski

You got your data structure wrong (yeah, indentation matters...) Anyway, here is solution for you:

你的数据结构错了(是的,缩进很重要......)无论如何,这里是你的解决方案:

def get_temp(lat, lon):
    data = urllib2.urlopen(
    "http://api.openweathermap.org/data/2.1/find/station?lat=%s&lon=%s&cnt=1"% (lat, lon))
    js_data = json.load(data)
    try:
        return js_data['list'][0]['main']['temp']
    except (KeyError, IndexError):
        return None

Output:

输出:

In [121]: get_temp(50,50)
Out[121]: 275.15