将循环变量传递给新函数

时间:2020-04-01 19:15:55

标签: python

我正在努力将变量传递给单独的函数。下面缺少什么?

import sys, os, locale
from workflow import Workflow, ICON_WEB, web
import xml.etree.ElementTree as ET
def main(wf):

    url = 'https://digitallibrary.un.org/search?ln=en&p=A/72/490&f=&rm=&ln=en&sf=&so=d&rg=50&c=United+Nations+Digital+Library+System&of=xm&fti=0&fti=0'
    r = web.get(url)
    r.raise_for_status()
    root = ET.fromstring(r.content)

    for record in root:
        symbol = record.find("{http://www.loc.gov/MARC21/slim}datafield[@tag='191']/{http://www.loc.gov/MARC21/slim}subfield[@code='a']").text
        report_title = record.find("{http://www.loc.gov/MARC21/slim}datafield[@tag='245']/{http://www.loc.gov/MARC21/slim}subfield[@code='a']").text
        wf.add_item(title=report_title,subtitle=symbol,icon=ICON_WEB)

    wf.send_feedback()

if __name__ == u"__main__":
    wf = Workflow()
    sys.exit(wf.run(main))

这将返回local variable references before assignment

1 个答案:

答案 0 :(得分:0)

删除for循环似乎解决了原始问题,并且通过G. Anderson的修复,代码现在可以使用了。