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 8368737
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T13:27:24+00:00 2026-06-09T13:27:24+00:00

I am writing a file parser and I want to be able to determine

  • 0

I am writing a file parser and I want to be able to determine witch “data fields” it would return for me.

I am starting to learn python and am still used to think like a Java programmer, so this question is more about how to design my module than about how specifically parse the file.

Contextualizing, each line of the file has a fixed number of characters and each information is contained between specific indexes. Eg.:

XX20120101NAME1CITYA
XY20120101NAME2CITYB

In this fictional example, from index 0 until 2 you have one information, from 2 to 10 another, and so on…

Using Java, I would normally create an enumerator representing the different pieces of information, each “storing” the start index and the end index. In my parsing class, I would then make an method available design to accept n different enums. Eg.:

enum FileInformation {
    INFO01(0,2), INFO02(2,10), INFO03(10,15), INFO04(15,20);
    int startIndex;
    int endIndex;

    public FileInformation(int si, int ei)  {
        this.startIndex = si;
        this.endIndex = ei;
    }

    public int getStartIndex() { return si; }
    public int getEndIndex() { return ei; }
}

public Whatever parse(FileInformation... infos) {
    // Here I would iterate through infos[], 
    // using its start and end index to retrieve only what I need.
}

I know that I probably should not use the same line of though in python, specially because the language wouldn’t allow it (no enums in python) and because I imagine python can be much less verbose, but I have no idea of a good design practice to achieve this same result.

It is valid to mention that I don’t want to expose the module’s user to unnecessary complexity, or to force him to know the indexes for each information. The module’s user should preferably be able to determine witch information he wants and its order.

So, do you have any insights about solving this requisites in an elegant manner?
Thanks in advance

  • 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-09T13:27:26+00:00Added an answer on June 9, 2026 at 1:27 pm

    Python already has a builtin type that does what FileInformation does – check out slice.

    Here’s how your module might look:

    # module dataparser.py
    
    INFO01, INFO02, INFO03, INFO04 = map(slice, ((0,2),(2,10),(10,15),(15,20)))
    
    def parse(infos, data):
        return [data[info] for info in infos]
    

    And how a calling module might use it:

    # module dataparser_user.py
    
    import dataparser as dp
    
    data = """\
    XX20120101NAME1CITYA
    XY20120101NAME2CITYB""".splitlines()
    
    for d in data:
        print d, dp.parse((dp.INFO01, dp.INFO03), d)
    
    # or use partial to define a function object that takes your 
    # subset number of slices
    from functools import partial
    specific_parse = partial(dp.parse, (dp.INFO01, dp.INFO03))
    
    for d in data:
        print d, specific_parse(d)
    

    If you were to implement your own enum analog in Python, I think namedtuple would be the closest thing (seeing as your Java enum has getters but no setters – namedtuples are likewise immutable):

    from collections import namedtuple
    FileInformation = namedtuple("FileInformation", "start end")
    INFO01, INFO02, INFO03, INFO04 = map(FileInformation, ((0,2),(2,10),(10,15),(15,20)))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a flat file parser that reads token/value pairs using a Scanner.
In context - I'm writing a simple, standalone config file parser for a class
How can I write data to stdout instead of writing file? I'm using GPG
I am writing a script for customising a configuration file. I want to replace
I wrote a file parser for a game I'm writing to make it easy
In writing a parser for a particular computational biology file format, I'm running into
I'm developing in python, still new to the game, and I want to make
I am writing a vcf parser and I have the file open but now
I'm writing a case statement to launch and xml parser activity and want to
first, sorry about my english, i still learning. I writing Python module for my

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.