Форум сайта python.su
Прошу помочь с парсером xml. Есть XML с такой структурой:
<root> <Result RC="0" /> <GetInvoiceInfo> <Invoice ContractNumber="somedata" Action="R" ShipmentNumber="somedata" ShipRefNum="somedata" PickUpType="C" ProductCode="somedata" InsuranceSum="somedata" DeclaredSum="0.00" CODGoodsSum="0.00" CODDeliverySum="0.00" SBits="somedata" OrderNumber="somedata" CurState="Обработка" CenterPay="" InsuranceType="INS" Description="22"> <Shipper PostCode="somedata" Country="somedata" Region="somedata" City="somedata" Address="somedata" CompanyName="somedata" ContactName="somedata" Phone=""/> <Receiver PostCode="somedata" Country="somedata" Region="somedata" City="somedata" Address="somedata" CompanyName="somedata" ContactName="somedata" Phone="somedata" ConsigneeCollect=""/> <SMS SMSNumberShipper=""/> <Pieces> <Piece Description="somedata" PieceID="somedata" ClientBarcode="" Weight="0.22" Length="0.20" Width="0.10" Depth="0.10" ClientWeight="0.40" Quantity="1"> <SubPieces> <SubPiece Description="somedata" Cost="somedata"/> </SubPieces> </Piece> </Pieces> </Invoice> </GetInvoiceInfo> </root>
import xml.etree.ElementTree as etree import MySQLdb INFile = 'xml/monitoring.xml' SQLtable = "invoice" MyHost="localhost" MyUser="root" MyPasswd="pass" MyDb="invoices" try: db = MySQLdb.connect(host=MyHost, user=MyUser,passwd=MyPasswd, db=MyDb, charset='utf8') cursor = db.cursor() tree = etree.parse(INFile) root = tree.getroot() getinvoiceinfos = root.find('GetInvoiceInfo') for getinvoiceinfo in getinvoiceinfos: dic = dict(getinvoiceinfo.attrib) dic.update(getinvoiceinfo.find('Shipper').attrib) Receiver = getinvoiceinfo.find('Receiver').attrib dic.update({ #'postcode10': Receiver['PostCode'], 'country10': Receiver['Country'], 'region10': Receiver['Region'], 'city10': Receiver['City'], 'address10': Receiver['Address'], 'companyname10': Receiver['CompanyName'], 'contactname10': Receiver['ContactName'], 'phone10': Receiver['Phone'], 'consigneecollect10': Receiver['ConsigneeCollect'], }) pieceses = getinvoiceinfo.find('Pieces') Piece = pieceses.find('Piece').attrib dic.update({ 'description10': Piece['Description'], #'pieceid': Piece['PieceID'], 'clientbarcode': Piece['ClientBarcode'], 'weight': Piece['Weight'], 'length': Piece['Length'], 'width': Piece['Width'], 'depth': Piece['Depth'], 'clientweight': Piece['ClientWeight'], 'quantity': Piece['Quantity'], }) for subpiece in root.iter('SubPiece'): SubPiece = subpiece.attrib dic.update({ 'description20': SubPiece['Description'], 'cost': SubPiece['Cost'], }) if dic.get('ShipRefNum') != None: dic.update({'shiprefnum':dic['ShipRefNum']}) del(dic['ShipRefNum']) if dic.get('FullDescription') != None: dic.update({'fulldescription':dic['FullDescription']}) del(dic['FullDescription']) if dic.get('DeliveryDT') != None: dic.update({'deliverydt':dic['DeliveryDT']}) del(dic['DeliveryDT']) if dic.get('PostCode') != None: dic.update({'postcode':dic['PostCode']}) del(dic['PostCode']) if dic.get('PostCode') != None: dic.update({'postcode10':dic['PostCode']}) del(dic['PostCode']) if dic.get('AgreedDate') != None: dic.update({'agreeddate':dic['AgreedDate']}) del(dic['AgreedDate']) if dic.get('CenterPay') != None: dic.update({'centerpay':dic['CenterPay']}) del(dic['CenterPay']) if dic.get('ConsigneeCollect') != None: dic.update({'consigneecollect10':dic['ConsigneeCollect']}) del(dic['ConsigneeCollect']) if dic.get('ClientBarcode') != None: dic.update({'clientbarcode':dic['ClientBarcode']}) del(dic['ClientBarcode']) if dic.get('Cost') != None: dic.update({'cost':dic['Cost']}) del(dic['Cost']) sql = """REPLACE INTO {tab} ({col}) VALUES ({val});""".format( tab=SQLtable col='`'+'`, `'.join(dic.keys()).lower()+'`', val="'"+"', '".join(dic.values()).replace('\\','/')+"' ") cursor.execute(sql) db.commit() finally: db.close()
for subpiece in root.iter('SubPiece'): SubPiece = subpiece.attrib dic.update({ 'description20': SubPiece['Description'], 'cost': SubPiece['Cost'], })
Офлайн
TruemanЧто выводится сейчас и что должно выводиться?
При добавлении в базу, дублируется одно и тоже значение во все записи.
TruemanДа не факт.
Проблема вот этой частью:
Отредактировано py.user.next (Фев. 14, 2017 05:40:12)
Офлайн
По-моему Trueman вы пытаетесь изобрести велосипед
import xmltodict xml = u""" <root> <Result RC="0" /> <GetInvoiceInfo> <Invoice ContractNumber="somedata" Action="R" ShipmentNumber="somedata" ShipRefNum="somedata" PickUpType="C" ProductCode="somedata" InsuranceSum="somedata" DeclaredSum="0.00" CODGoodsSum="0.00" CODDeliverySum="0.00" SBits="somedata" OrderNumber="somedata" CurState="Обработка" CenterPay="" InsuranceType="INS" Description="22"> <Shipper PostCode="somedata" Country="somedata" Region="somedata" City="somedata" Address="somedata" CompanyName="somedata" ContactName="somedata" Phone=""/> <Receiver PostCode="somedata" Country="somedata" Region="somedata" City="somedata" Address="somedata" CompanyName="somedata" ContactName="somedata" Phone="somedata" ConsigneeCollect=""/> <SMS SMSNumberShipper=""/> <Pieces> <Piece Description="somedata" PieceID="somedata" ClientBarcode="" Weight="0.22" Length="0.20" Width="0.10" Depth="0.10" ClientWeight="0.40" Quantity="1"> <SubPieces> <SubPiece Description="somedata" Cost="somedata"/> </SubPieces> </Piece> </Pieces> </Invoice> </GetInvoiceInfo> </root> """ dct = xmltodict.parse(xml)
Офлайн
Вопрос решился, вместо той проблемной части, что я писал выше, надо
piece_element = pieceses.find('Piece') for subpiece in piece_element.iter('SubPiece'): SubPiece = subpiece.attrib dic.update({ 'description20': SubPiece['Description'], 'cost': SubPiece['Cost'], })
Офлайн