简体   繁体   中英

Reading dictionary structure from text file in python

I have file where I have defined python dictionary:

dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First', 'Friends': {'Emil':1, 'Frank':0} };

I want to read this dictionary and use it's element in python code. I've end up with following code:

#!/usr/bin/python

import sys
import os
import ast
import re
from pprint import pprint as pp

def readObjFromFile(file):
  with open (file, "r") as myfile:
    data=myfile.read()
  data = re.sub("^#.*", "", data)
  data = data.replace('\n', '')
  data = data.split("=", 1)[1].split(";", 1)[0].lstrip()
  data = ast.literal_eval(data)
  return data

if __name__ == "__main__":
  if len(sys.argv[1:]) == 1:
    dict = readObjFromFile(sys.argv[1])
    print type(dict)
    pp(dict)
  else:
    print "Pass file from which object will be read"
    exit

This works also for this larger dictionary. I want to ask if there is better way to do it? I am aware of pickle module but this is not what I want because it stores data in it's own format. I was considering modules which are used for parsing json due to similarity with python dicts, but I do not know if this approach is safe.

I'm assuming that you don't have control over the file format and are being handed it from somewhere. It's close enough to json that I'd aim to make it json. I'd do something like the following:

lines = []
with open(file) as f:
    for line in f:
        if line[0] == "#":
            continue
        l = line.strip().split("=")[-1].strip(";")
        lines.append(re.sub("'", "\"", l)
return json.loads("".join(lines))

The file you have really represents a human interpreted version of the dictionary. We can read it and see how there is a variable that we want to assign to a dictionary. What you really want to do is store the dict in a programatic friendly format. And JSON is a perfect use of this format. Other formats include XML and YAML, but Python natively and easily will read JSON input.

If you look closely at your example, you see that the sample data set has a dictionary with a nested dictionary. JSON is built for these kinds of use cases. So a JSON file with your data above would look like:

{
    "Name": "Zara", 
    "Age": 7, 
    "Class": "First", 
    "Friends": {
        "Emil":1, 
        "Frank":0
    } 
}

Note the sub-dictionary is part of the structure of the JSON file. Also note that double quotes are used.

Now you can easily have python do your work for you:

import json
from os import open
from pprint import pprint

data = open(filename).read()
d = json.loads(data)
pprint(d)

And you can access the submodules by using:

d['Friends]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM