Уведомления

Группа в Telegram: @pythonsu

#1 Фев. 13, 2017 09:53:59

Trueman
Зарегистрирован: 2017-02-13
Сообщения: 4
Репутация: +  0  -
Профиль   Отправить e-mail  

Парсер xml

Прошу помочь с парсером 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'],
            })
При добавлении в базу, дублируется одно и тоже значение во все записи.
Подскажите как исправить?

Офлайн

#2 Фев. 14, 2017 05:39:34

py.user.next
От:
Зарегистрирован: 2010-04-29
Сообщения: 9726
Репутация: +  843  -
Профиль   Отправить e-mail  

Парсер xml

Trueman
При добавлении в базу, дублируется одно и тоже значение во все записи.
Что выводится сейчас и что должно выводиться?

Trueman
Проблема вот этой частью:
Да не факт.



Отредактировано py.user.next (Фев. 14, 2017 05:40:12)

Офлайн

#3 Фев. 14, 2017 05:52:47

FishHook
От:
Зарегистрирован: 2011-01-08
Сообщения: 8312
Репутация: +  568  -
Профиль   Отправить e-mail  

Парсер xml

По-моему 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)



Офлайн

#4 Фев. 14, 2017 22:50:26

Trueman
Зарегистрирован: 2017-02-13
Сообщения: 4
Репутация: +  0  -
Профиль   Отправить e-mail  

Парсер xml

Вопрос решился, вместо той проблемной части, что я писал выше, надо

  piece_element = pieceses.find('Piece')
        for subpiece in piece_element.iter('SubPiece'):
            SubPiece = subpiece.attrib
            dic.update({
                'description20': SubPiece['Description'],
                'cost': SubPiece['Cost'],
            })

FishHook спасибо, учту на будущее.

Офлайн

Board footer

Модераторировать

Powered by DjangoBB

Lo-Fi Version