1

I have a large number of name value pair that I need to use in a program. What should be the best way to store the name value pair in a file so that I can easily parse them in my python program. For some reasons we are not using xml. Please suggest any good method to store them in a file and use them.

P.S -- The people writing the name value pairs in file need not be developers. They will be told to write certain values and they'll type. So i want to keep it as simple as possible.

Abhishek
  • 1,717
  • 6
  • 22
  • 39
  • 2
    Probably, you can use [pickle](http://docs.python.org/2/library/pickle.html) module. – user2155932 Mar 18 '13 at 06:36
  • Regarding PS, yes I understand. Anyway we need to train them to write something right? Then it shouldn't be a difficult task. IMHO. :) – Babu Mar 18 '13 at 06:51

3 Answers3

1

a.csv
name1,value1
name2,value2

import csv
[i for i in csv.reader(open('a.csv').read().splitlines())]

[['name1', 'value1'], ['name2', 'value2']]

Arty
  • 579
  • 1
  • 8
  • 17
0

You can use JSON (recommended) in python:

import json

http://docs.python.org/2/library/json.html

or cvs

enter http://docs.python.org/2/library/csv.html

nomaka
  • 58
  • 5
  • I had csv in mind. I planned like this name1,value1,name2,value2,name3,value3, ...... If you feel you can add more structure to it, you are welcome. Json is a good option but again csv can be written via excel by any person. – Abhishek Mar 18 '13 at 06:46
0

You can store it as a python dict inside .py file itself and just import inside your script. If you'll come to know about the file name only at runtime use __import__ to import the same. Ref here. By this way, you don't need to do any parse or conversion.

Babu
  • 2,548
  • 3
  • 30
  • 47
  • 1
    to elaborate on this, please consider also http://docs.python.org/2/library/functions.html#execfile as discussed here: http://stackoverflow.com/a/1027739/582906 but keeping in mind python3 compatibility: http://stackoverflow.com/questions/6357361/alternative-to-execfile-in-python-3-2 – furins Mar 18 '13 at 06:46