python函数中声明全局变量_在python中声明函数中的全局变量

本文介绍了一个Python程序,该程序负责解析XML文件,并将解析出的数据更新到MySQL数据库中。文章探讨了解析过程中的变量传递问题及解决方案。

我创建的程序将用于解析xml文件并将解析数据放入数据库。虽然我的代码现在可以运行了,但我的导师在我的代码中有一个注释,顺便说一句,这是我的代码:import os

import time

import MySQLdb

import ConfigParser

import elementtree.ElementTree as ET

def update_database(article_code, date_received, s100rsd, remark_text, db):

cur = db.cursor()

try:

cur_query = cur.execute("""INSERT INTO tblS100CurrentListing """

"""(article_Code, dateReceived, s100RSD, remarks) VALUES (%s, %s, %s, %s) """

"""ON DUPLICATE KEY UPDATE revisedRSD = %s, remarks = %s """,

(article_code, date_received, s100rsd, remark_text, s100rsd, remark_text))

db.commit()

except MySQLdb.Error, e:

print "An error has been passed %s" %e

db.rollback

rows_affected = cur.rowcount

if rows_affected > 0:

print "Changes made in the database"

else:

print "Nothing is change in the database"

def parse_xml(source_path, xml_file):

# Alvin: !!! globals?

global article_code

global date_received

global s100rsd

global remark_text

article_code = xml_file.split('.')[0]

tree = ET.parse(xml_file)

root = tree.getroot()

order = root.find('order')

order_time = order.find('time')

year = order_time.attrib['yr']

month = order_time.attrib['month']

day = order_time.attrib['day']

hour = order_time.attrib['hr']

min = order_time.attrib['min']

sec = order_time.attrib['sec']

date_received = year + month + day + hour + min + sec

due_date = order.find('due-date')

due_date_time = due_date.find('time')

yr = due_date_time.attrib['yr']

month = due_date_time.attrib['month']

day = due_date_time.attrib['day']

s100rsd = "%s-%s-%s" %(yr, month, day)

item_info = order.find('item-info')

item_remarks = item_info.find('item-remarks')

item_remark_list = item_remarks.findall('item-remark')

item_remark_len = len(item_remark_list) - 1

item_remark = item_remark_list[item_remark_len]

remark = item_remark.find('remark')

remark_text = remark.text

def main():

config = ConfigParser.ConfigParser()

config.readfp(open('part4b.ini'))

server = config.get('main', 'Server')

port = config.get('main', 'Port')

port = int(port)

schema = config.get('main', 'Schema')

table = config.get('main', 'Table')

user = config.get('main', 'User')

password = config.get('main', 'Password')

source_path = config.get('main', 'filepath')

db = MySQLdb.connect(server, user, password, schema, port)

xml_list = os.listdir(source_path)

for xml_file in xml_list:

if xml_file.endswith('.xml'):

parse_xml(source_path, xml_file)

update_database(article_code, date_received, s100rsd, remark_text, db)

db.close()

print "This will close after 2 seconds . ."

time.sleep(2)

if __name__ == '__main__':

main()

在parse_xml函数中,他不希望我在其中使用全局变量。如何在main中使用这些变量而不声明为全局变量?在

谢谢你的帮助。在

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值