from simplified_scrapy import SimplifiedDoc, utils #simplified_scrapy is framework for extrcting data xml = utils.getFileContent('check_in.xml') doc = SimplifiedDoc(xml) #SimplifiedDoc is a library for parsing data such as HTML and XML nodes = doc.select('air:EDS_AirCheckInRQ').children print (nodes.tag) for x in nodes: print((doc.select('air:EDS_AirCheckInRQ')['Version']),(doc.select('com:Source'))) Here is my output:- 4.000 {'tag': 'com:Source', 'AirlineVendorID': 'CM'} 4.000 {'tag': 'com:Source', 'AirlineVendorID': 'CM'} 4.000 {'tag': 'com:Source', 'AirlineVendorID': 'CM'} 4.000 {'tag': 'com:Source', 'AirlineVendorID': 'CM'} 4.000 {'tag': 'com:Source', 'AirlineVendorID': 'CM'} 4.000 {'tag': 'com:Source', 'AirlineVendorID': 'CM'}這是我的 python 代碼,我只想在其中打印一次,但是當我運行它時,它會給出打印多次的輸出。我只想打印一次。誰能幫我解決為什么打印多次而不是一次?
2 回答

慕姐8265434
TA貢獻1813條經驗 獲得超2個贊
如果你有你的print()
inside ,for x in nodes:
它會打印每個x
in nodes
。只需刪除 for 循環,您甚至沒有x
在循環中使用。

江戶川亂折騰
TA貢獻1851條經驗 獲得超5個贊
您正在循環打印。擺脫這個:
for?x?in?nodes: ????????print((doc.select('air:EDS_AirCheckInRQ')['Version']),(doc.select('com:Source')))
據我了解,您正在嘗試從節點中提取一些信息您可以嘗試以下操作:
for?x?in?nodes: ????????print((x.select('air:EDS_AirCheckInRQ')['Version']),(x.select('com:Source')))
但這可能不起作用,因為我不知道您正在使用哪些數據。
添加回答
舉報
0/150
提交
取消