Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6623949
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:35:36+00:00 2026-05-25T21:35:36+00:00

I have a python 3 script below that is supposed to download an xml

  • 0

I have a python 3 script below that is supposed to download an xml file and split it into smaller files with only 500 items each. I am having two problems:

  1. the last item in the original xml is not present in the split files
  2. if the original xml was 1000 items long it will create a 3rd empty xml file.

Can anyone tell me where there could be such an error in my code to cause these symptoms?

import urllib.request as urllib2
from lxml import etree

def _yield_str_from_net(url, car_tag):

    xml_file = urllib2.urlopen(url)
    for _, element in etree.iterparse(xml_file, tag=car_tag):
        yield etree.tostring(element, pretty_print=True).decode('utf-8')
        element.clear()

def split_xml(url, car_tag, save_as):

    output_file_num = 1
    net_file_iter = _yield_str_from_net(url, car_tag)
    while True:
        file_name = "%s%s.xml" % (save_as, output_file_num)
        print("Making %s" % file_name)
        with open(file_name, mode='w', encoding='utf-8') as the_file:
            for elem_count in range(500):    # want only 500 items
                try:
                    elem = next(net_file_iter)
                except StopIteration:
                    return
                the_file.write(elem)
                print("processing element #%s" % elem_count)
        output_file_num += 1

if __name__ == '__main__':
    split_xml("http://www.my_xml_url.com/",
              'my_tag',
              'my_file')
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T21:35:37+00:00Added an answer on May 25, 2026 at 9:35 pm

    The second one is no error but by design. After reading 1000 elements the iterator does not yet know that there is no further item and thus continues with the while True loop.

    It would be great if iterators would have a hasNext then you could replace it by while hasNext in order to overcome this issue. Unfortunately there is no such thing in python.

    For the first question: currently I can’t see anything in your code explaining this issue.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a python script which outputs lots of data, sample is as below.
I have a Python script I recently wrote that I call using the command
I have a python script that analyzes a set of error messages and checks
I have a Python script that needs to execute an external program, but for
I have a python script that is a http-server: http://paste2.org/p/89701 , when benchmarking it
I have a python script that I would like to add a Shutdown when
I have a python script that has to launch a shell command for every
I have a Python script that calls an executable program with various arguments (in
I have a python script that, once executed from command line, performs the needed
I have a Python script that uses built-in modules but also imports a number

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.