Minhaj Khan
Minhaj Khan

Reputation: 17

Print multiple times in python?

    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'}

Here is my python code in which I want to print only once, but when I run this it gives me output which prints multiple times. I want to print only once. Can anyone help me out why this print multiple times instead of once?

Upvotes: 1

Views: 577

Answers (2)

Benjamin Philip
Benjamin Philip

Reputation: 198

Like what Adamantoisetortoise said, you are printing in a loop. Get rid of this:

for x in nodes:
        print((doc.select('air:EDS_AirCheckInRQ')['Version']),(doc.select('com:Source')))

From what I understand, You are trying to extract some information from nodes You could try this:

for x in nodes:
        print((x.select('air:EDS_AirCheckInRQ')['Version']),(x.select('com:Source')))

But this may not work as I don't know what data you are working with.

Upvotes: 1

JLeno46
JLeno46

Reputation: 1269

If you have your print() inside for x in nodes: it will print for every x in nodes. Just remove your for loop, you're not even using x in your loop.

Upvotes: 1

Related Questions