转让前MySQLdb的错误局部变量引用(与平时的不同) [英] MySQLdb error local variable referenced before assignment (different than usual)

查看:128
本文介绍了转让前MySQLdb的错误局部变量引用(与平时的不同)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过遍历返回一个JSON文件中的API添加一些数据到MySQL数据库进行迭代。我使用Python和MySQLdb的模块。

出于某种原因,我得到了臭名昭著UnboundLocalError。我看了看其他方案时,这个问题发生,并且已经回答了计算器,但没有与我共鸣,我不能直接将其应用到这个问题。

下面是我的code:

 高清请求(API_KEY,网址,CH_NO):
    URL = URL + CH_NO +'/请求
    请求= requests.get(URL,AUTH =(API_KEY,''))
    data_dict = request.json()
    data_dict_json_dumps = json.dumps(data_dict)
    数据= json.loads(data_dict_json_dumps)    尝试:
        在数据['项目']项目:
            返回(项目['标签']项['created_on']项['delivered_on']项['satisfied_on']项['状态']项['细节'] ['说明'],项目['persons_entitled'] [0] ['名'])
    除了KeyError异常:
        通过    尝试:
        说明=项目['细节'] ['说明']
    除了KeyError异常:
        说明=无
    尝试:
        persons_entitled =项目['persons_entitled'] [0] ['名']
    除了KeyError异常:
        persons_entitled =无    尝试:
        cursor.execute(INSERT INTO companies_and_charges_tmp(标签,COMPANY_ID,创建,交付,满意,状态,说明,persons_entitled)VALUES(%S,%S,%S,%S,%S,%S,%S, %S),,(item.get(标记),CH_NO,item.get('created_on'),item.get('delivered_on'),item.get('satisfied_on'),item.get( 状态),描述persons_entitled))
    db.commit()    最后:
        time.sleep(0.5)    德尔数据在ch_nos CH_NO:
    charges_request(API_KEY,网址,CH_NO)

和这里的完整的错误:

 回溯(最后最近一次调用):
  文件test2.py58行,上述<&模块GT;
    charges_request(API_KEY,网址,CH_NO)
  文件test2.py49行,在charges_request
    cursor.execute(INSERT INTO companies_and_charges_tmp(ETag的,COMPANY_ID,创建,交付,满意,状态,说明,persons_entitled)VALUES(%S,%S,%S,%S,%S,%S,%S, %S),,(item.get('ETAG'),CH_NO,item.get('created_on'),item.get('delivered_on'),item.get('satisfied_on'),item.get( 状态),描述persons_entitled))
UnboundLocalError:局部变量'项目'分配之前引用


解决方案

的问题是,项目当你进入对于只分配循环

 在数据['项目']项目:
    ...

如果数据['项目'] 是空的,你从来没有这样做,而项目未分配

I am attempting to add some data to MySQL database via a loop that iterates through the API that returns a JSON file. I am using Python and MySQLdb module.

For some reason I am getting the infamous UnboundLocalError. I looked at the other scenarios when this problem occurred and that have already been answered on StackOverflow but nothing resonated with me and I couldn't apply it directly to this problem.

Here's my code:

def request(API_KEY, URL, ch_no):
    url = URL + ch_no + '/request'
    request = requests.get(url, auth=(API_KEY, ''))
    data_dict = request.json()
    data_dict_json_dumps = json.dumps(data_dict)
    data = json.loads(data_dict_json_dumps) 

    try:
        for item in data['items']:
            return (item['tag'], item['created_on'], item['delivered_on'], item['satisfied_on'], item['status'], item['particulars']['description'], item['persons_entitled'][0]['name'])   
    except KeyError:
        pass

    try:
        description = item['particulars']['description']
    except KeyError:
        description = None
    try:
        persons_entitled = item['persons_entitled'][0]['name']
    except KeyError:
        persons_entitled = None 

    try:
        cursor.execute("""INSERT INTO companies_and_charges_tmp (tags, company_id, created, delivered, satisfied, status, description, persons_entitled) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)""", (item.get('tag'), ch_no, item.get('created_on'), item.get('delivered_on'), item.get('satisfied_on'), item.get('status'), description, persons_entitled))
    db.commit()

    finally: 
        time.sleep(0.5)

    del data

for ch_no in ch_nos:
    charges_request(API_KEY, URL, ch_no)

And here's the full error:

Traceback (most recent call last):
  File "test2.py", line 58, in <module>
    charges_request(API_KEY, URL, ch_no)
  File "test2.py", line 49, in charges_request
    cursor.execute("""INSERT INTO companies_and_charges_tmp (etags, company_id, created, delivered, satisfied, status, description, persons_entitled) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)""", (item.get('etag'), ch_no, item.get('created_on'), item.get('delivered_on'), item.get('satisfied_on'), item.get('status'), description, persons_entitled))
UnboundLocalError: local variable 'item' referenced before assignment

解决方案

The issue is that item is only assigned when you enter the for loop

for item in data['items']:
    ...

If data['items'] is empty, you never do so, and item remains unassigned.

这篇关于转让前MySQLdb的错误局部变量引用(与平时的不同)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆