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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T04:39:38+00:00 2026-05-16T04:39:38+00:00

I need to parse some text file, create objects for various entities encountered in

  • 0

I need to parse some text file, create objects for various entities encountered in the text, and put them in some data structure (e.g., a list) for further processing. Example of the text:

laptop
 17" dell, weight: 12 lb
desktop
 24" hp

I know in advance which entities may exist in the text, and what attributes they are supposed to have. In this example, I would already have classes laptop and desktop defined (probably subclasses of class computer). The parser would just need to create objects laptop(‘dell’, 17, 12), and dekstop(‘hp’, 24).

If I follow this route, I would need to retrieve class names from strings, and create objects of those classes. Is it the Pythonic way of doing things? If so, what’s the best approach (using Python 3.1)? If not, what should I do instead?

Thanks!

What

  • 1 1 Answer
  • 1 View
  • 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-16T04:39:39+00:00Added an answer on May 16, 2026 at 4:39 am

    If the classes are defined in computers.py, say, you can do

    import computers
    getattr( computers, "Laptop" )( <params> )
    

    to instantiate a computers.Laptop. If they are defined in the same file that you are running the code in (so that they are global variables), you can do

    globals()[ "Laptop" ]
    

    but this is less elegant; it would be nicer to put them in a separate scope.

    Alternatively, if you want a more powerful mapping (say you want “Nettop”, “Lapbook”, and “Laptop” all to instantiate Laptop), you could maintain a mapping of strings to their corresponding constructor and use that:

    mapping = { "Laptop": Laptop, "Nettop": Laptop, ... }
    mapping[ "Laptop" ]()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to parse Windows text file and extract all data related to operations.
I need to parse some text in a UITextField and turn it into a
I need parse through a file and do some processing into it. The file
Need to parse a file for lines of data that start with this pattern
I need help to parse some information from a mass of text, basically I
I'm trying to parse a text file word by word and I need to
Having the following hierarchical text data input (JunOS-like, in fact) I need to parse
I am newbie to Perl. I need to parse a tab separated text file.
I have a text file that I parse each month and insert the data
I have a text file having some data in following format %app_lookup_strings = (

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.