简体   繁体   中英

Creating a dictionary of a particular row in a csv file

Suppose my file is somewhat in this way:

['720',
'717',
'"Diagnostic"',
'487',
'"{""status"": ""active""',
'""division_type"": ""Organisation""}"']

I need to select 487 as the key in a new dictionary and the words following 487 as it is. Basically a dictionary within a new dictionary. I have tried out the following code:

for row in line:
    key = row[3]
    if key in d:
         pass
    d[key]=row[21:]
print(d)

I chose 3 because 487 is the 3rd index and i chose 21 because in the csv file the following line is in the row number 21.

I am a newbie to programming. Please help me out. The error in the message is: index is out of range

I'd say without further data the following approach is more or less experimental but might be a good starting point. You could look for the key in question ( 487 in your case) and for consecutive curly braces:

import re
from ast import literal_eval

file = """
['720',
'717',
'"Diagnostic"',
'487',
'"{""status"": ""active""',
'""division_type"": ""Organisation""}"']"""

rx = re.compile(r'(?P<key>487)[^{}]+(?P<content>\{[^{}]+\})')

for m in rx.finditer(file):
    content = re.sub(r"""'?"+'?""", '"', m.group('content'))
    d = {m.group('key'): literal_eval(content)}
    print(d)

This yields

{'487': {'status': 'active', 'division_type': 'Organisation'}}

Or, more general, as a function:

def make_dict(string, key):
    rx = re.compile(r'(?P<key>' + key + ')[^{}]+(?P<content>\{[^{}]+\})')

    for m in rx.finditer(string):
        content = re.sub(r"""'?"+'?""", '"', m.group('content'))
        yield {m.group('key'): literal_eval(content)}

for d in make_dict(file, '487'):
    print(d)

In general, fix the input format of the file!

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