【问题标题】:SyntaxError: invalid syntax (<string>)SyntaxError: 无效的语法 (<string>)
【发布时间】:2014-03-16 22:09:46
【问题描述】:

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

代码:

    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;

我也试过这个版本,但它不起作用

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;

【问题讨论】:

  • 前 3 行应该是 Python 的一部分吗?最后一行?因为那不是 Python。
  • 如果它 Python,其余部分也是错误的缩进。
  • 是 sql 和 python。代码有什么问题?
  • 请在每个: 之后进行中断和缩进,因为它应该在 Python 中完成。您很有可能会发现代码出了什么问题,甚至可能足以让它正常工作。
  • @m.wasowski 我打破了.. 但它仍然无法正常工作

标签: python python-2.7 postgresql-9.1 postgis plpython


【解决方案1】:

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

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;

请注意,上面是4-space indent convention (PEP 8)。如果您是 Python 新手,我建议您阅读一些教程来了解缩进的语法和使用。

【讨论】:

    【解决方案2】:

    您的数据结构有误(是的,缩进很重要...) 无论如何,这是给你的解决方案:

    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
    

    输出:

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

    【讨论】:

    • DETAIL: IndentationError: expected an indented block (&lt;string&gt;, line 10)
    • 签入纯python,它工作正常 - 并返回正确的结果。我没有在 postgresql 中嵌入脚本的经验,所以我不会猜测您为什么会收到此错误。我建议阅读有关该主题的 postgresql 文档。
    猜你喜欢
    • 2015-09-08
    • 1970-01-01
    • 2013-12-19
    • 2020-07-30
    • 1970-01-01
    • 2018-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多