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

  • Home
  • SEARCH
  • 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 7871947
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T02:03:52+00:00 2026-06-03T02:03:52+00:00

Firstly: sorry about my pathetic english language skill. Secondly: i have learnt python for

  • 0

Firstly: sorry about my pathetic english language skill.
Secondly: i have learnt python for few weeks, so please be patient.:)

My project:
I got a directory with multiple files (xml).

they looks like:    
            thingy1_442354565666.xml
            thingy2_502354565666.xml
            thingy3_692354565666.xml
            etc.

The numbers in filename represent the files last modification date. The bigger number means recent files. (This will be important later.)

Each xml contains two element.

for example:
        <period>012012</period>     # this represent months
        <charges>1098</charges>     # EUR

The problem:
I need an output txt with from each month.

like this:  
        jan: 1098
        feb: 499
        etc.

Unfortunately I have more than 12 xml in that directory, so i got 2-3 files (I always need the last modified file) in each months.

I tried to make a dictionary with filenames and period element, but i totaly lost.
Btw i can parse xml with elementtree etc., but i dont know how to choose the last modified file from each month.

Please help me out, and ask if i was not understandable.
Thanks!

  • 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-06-03T02:03:54+00:00Added an answer on June 3, 2026 at 2:03 am

    If larger numbers mean older files, sort the file list and iterate the files from smallest to largest (i.e. newest files first).

    Parse the xml and keep track of the (year, month) pairs you have seen. If you have already seen a pair, then you have already processed the newest file for that month and you can ignore subsequent files.

    Something like:

    import glob
    from datetime import datetime
    from lxml import etree
    
    def file_timestamp(filepath):
        """Return the timestamp from a file name"""
        filename = os.path.split(filepath)[1]
        filename = os.path.splitext(filename)[0]
        if '_' in filename:
            return filename.split('_')[1]
        return None
    
    class Datafiles(object):
        def __init__(self, dir_path):
            self.date_cache = {}
            self.dir_path = dir_path
    
        def __iter__(self):
            return self.files()
    
        def next(self):
            return self.files().next()
    
        def files(self):
            filepaths = glob.glob(self.dir_path + '*_*.xml')
            filepaths = sorted(filepaths, key=file_timestamp)
    
            for filepath in filepaths:
                ## Parse data (xml)
                data = self.parse_file(filepath)
                ## If year/month is seen before, skip
                date = datetime.strptime(data['period'].text, '%m%Y')
                month = (date.year, date.month)
                if month in self.date_cache:
                    continue
    
                ## Else store date and yield
                self.date_cache[month] = filepath
                yield data
    
        def parse_file(self, filepath):
            # YOUR XML PARSING HERE
            return etree.parse(filepath)
    

    Usage:

    >>> files = Datafiles(data_directory)
    >>> for xml_data in files:
    ...     ## do something with the data
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Firstly sorry about my grammar. I have two ImageViews and I want to swap
Firstly, sorry for my English (I'm from China). By reading this article( how to
Firstly, Modifying may be the wrong term, I see a few people have posted
Firstly, sorry about the question title, it may not be very descriptive for my
Firstly I asked sorry if this is a stupid question. but I have a
Firstly, sorry for my flawed english and being dumb :O I just want to
Firstly, sorry for my English. I'm Brazilian guy that is improving yet. I want
Firstly - sorry for my English.. I've inherited a rather big project which uses
Firstly, I feel sorry about the title, I do not know how to describe
Firstly, sorry about the long question... but this is doing my head in. Any

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.