简体   繁体   中英

Converting lines of a file containing arrays to a list of dictionaries

I have a file, each line of which contains data that I would like to read into a dictionary, resulting in a list of dictionaries. Or a dictionary of dictionaries, keyed by the first element from each line. The first element of each line is the only one that I can guarantee will be of the same type from line to line, ie it's a name. The data in the file looks something like this:

name:value1, var2:('str1', 'str2','str3'), var3:[0.1,1,10] , var4:range(1,10)
name:value2, var5:('str1', 'str2'), var6:range(1,10)

And I'd like to have it end up something like this:

dictionaryList=[
{"name": "value1", "var2":('str1', 'str2','str3'), var3:[0.1,1,10] , var4:range(1,10)},
{name:value2, var5:('str1', 'str2'), var6:range(1,10)}
] 

There's a number of questions about reading lines into elements of a single dictionary or reading a file into a nested dictionary . They all rely on splitting the line on a comma though. ie

content = f.readlines()
    for line in content:
        line = line.strip('\r').strip('\n').split(':')

If I do that, I end up with breaks in the ranges and arrays and wotnot. I was borderline going to use : as a separator, but that feels like horribly bad form and I have no way to automatically convert the correct commas to colons when I get sent more data. Is there a way to get around this?

f = open("test.txt","r")
lines = f.readlines()
f.close()
dictList = []
stack = []
brack = "([{"
opbrack = ")]}"
for line in lines:
    d = {}
    key = ""
    val = ""
    k = 0
    for i in line:
        if i == ',':
            if not(stack) or stack[-1] not in brack:
                d[key] = val
                key = ""
                val = ""
                k = 0
            elif k==0:
                key += i
            else:
                val += i
        elif i in brack:
            stack.append(i)
            if k==0:
                key += i
            else:
            val += i
        elif i in opbrack:
            stack.pop()
            if k==0:
                key += i
            else:
                val += i
        elif i == ":":
             if not(stack) or stack[-1] not in brack:
                k = 1
            elif k==0:
                key += i
            else:
                val += i
        else:
            if k==0:
                key += i
            else:
                val += i
    dictList.append(d)
print dictList

This code should do what you need. It assumes that the file is already in proper format.

This is the output:

[{'name': 'value1', ' var2': "('str1', 'str2','str3')", ' var3': '[0.1,1,10] '}, {' var5': "('str1', 'str2')", 'name': 'value2'}]

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