2

I feel my recently gained knowledge is still not sufficient when it comes to string processing. Please help me solve the below problem statement: (Please note:This is the simpler version of my requirement)

So.. I have a file (myoption) with content as below:

day=monday,tuesday,wednesday
month=jan,feb,march,april
holiday=thanksgiving,chirstmas

My python script should be able to read the file and process the read info such that in the end i have three list variables as below:

day --> ['monday','tuesday','wednesday']
month --> ['jan','feb','march','april']
holiday --> ['thanksgiving','christmas']

Please note: As per my requirement, the format for contents in myoption file should be simple. Hence You are free to modify the format of 'myoption' file without changing the content - This is to give you some flexibility.

Thanks :)

3
  • Why not stick with a classic format and just use ConfigParser? Commented Nov 21, 2011 at 6:50
  • @Raymond Thanks for informing about configparser, I will look into it. Commented Nov 21, 2011 at 6:58
  • @AnimeshSharma I like the confiparser answer, anyway I suggested you how to do it with your format Commented Nov 21, 2011 at 6:59

4 Answers 4

5

If you use the standard ConfigParser module your data would need to be in INI file format, and so would look something like this:

[options]
day = monday,tuesday,wednesday
month = jan,feb,march,april
holiday = thanksgiving,christmas

And then you could read the file as follows:

import ConfigParser

parser = ConfigParser.ConfigParser()
parser.read('myoption.ini')
day = parser.get('options','day').split(',')
month = parser.get('options','month').split(',')
holiday = parser.get('options','holiday').split(',')
Sign up to request clarification or add additional context in comments.

1 Comment

Is there anyway to append new data into the INI file using configparser? Using .set I am able to add data, but it overwrites old file. Hence If i am appending new data to old INI file - as of now i Have to add new and old data, which requires additional resources.
2

Here's a simple answer:

s = 'day=monday,tuesday,wednesday'
mylist = {}
key, value = s.split('=')
mylist[key] = value.split(',')

print mylist['day'][0]

Output: monday

4 Comments

Better getting used to not using list as a variable name, it hides the builtin and may lead to hard to debug bugs.
Thanks, I was wondering about that after I posted the answer, re-editing.
Thanks Alvin for your time. Now i know two solutions to my problem :)
I'd advocate ConfigParser as the first choice (ie: do not reinvent the wheel whenever possible :).
1

You may be interested in the ConfigParser module.

Comments

1

You can rewrite your file in YAML or XML if you want your application respect a standard. Anyway, if you want to keep your simple format, this should word:

Given the file data:

day=monday,tuesday,wednesday
month=jan,feb,march,april
holiday=thanksgiving,chirstmas

I propose this python script

f = open("data")
for line in f:
  content = line.split("=")
  #vars to set the variable name as the string found and 
  #[:-1] to remove the new line character
  vars()[content[0]] = content[1][:-1].split(",")
print day
print month
print holiday
f.close()

The output is

python main.py 
['monday', 'tuesday', 'wednesday']
['jan', 'feb', 'march', 'april']
['thanksgiving', 'chirstmas']

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.