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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:57:53+00:00 2026-05-27T12:57:53+00:00

I am writing a Python class to model a process and I want to

  • 0

I am writing a Python class to model a process and I want to initialized the parameters from a file, say 'input.dat'. The format of the input file looks like this.

'input.dat' file:

Z0: 0 0
k: 0.1
g: 1
Delta: 20
t_end: 300

The code I wrote is the following. It works but appears redundant and inflexible. Is there a better way to do the job? Such as a loop to do readline() and then match the keyword?

def load(self,filename="input.dat"):
    FILE = open(filename)
    s = FILE.readline().split()
    if len(s) is 3:
        self.z0 = [float(s[1]),float(s[2])] # initial state
    s = FILE.readline().split()
    if len(s) is 2:
        self.k = float(s[1])    # kappa
    s = FILE.readline().split()
    if len(s) is 2:
        self.g = float(s[1])
    s = FILE.readline().split()
    if len(s) is 2:
        self.D = float(s[1])    #  Delta
    s = FILE.readline().split()
    if len(s) is 2:
        self.T = float(s[1])    # end time
  • 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-27T12:57:53+00:00Added an answer on May 27, 2026 at 12:57 pm
    • If you are open to some other kind of file where you can keep your parameters, I would suggest you to use a YAML file.
    • The Python library is PyYAML. This is how you can easily use it with Python.
    • For a better introduction, look at this Wikipedia article: http://en.wikipedia.org/wiki/YAML.
    • The benefit is you can read the parameter values as lists or maps.
    • You would love it!
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a Player model class in Python with Django, and I've ran
I'm writing a Python class in C and I want to put assertions in
I am writing a class in Python 2.6.2 that contains a lookup table. Most
I'm using Eclipse for writing Python, and I want to be able to easily
I'm writing a Python program with a lot of file access. It's running surprisingly
I have a simple Python/Django class: class myModel(models.Model): date = models.DateTimeField() value = models.IntegerField()
I'm writing python package/module and would like the logging messages mention what module/class/function they
I'm writing some serialization/deserialization code in Python that will read/write an inheritance hierarchy from
I'm writing a grading robot for a Python programming class, and the students' submissions
I'm writing a python class to parse XML and pull out tag names and

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.