crypticgamer
crypticgamer

Reputation: 101

Convert multiline "tabular" string to dictionary

I have a string that looks something like this:

name1                            pass  blue  n/a
name-6t56-yt6                    fail  red   n/a
name-45                          pass  blue  n/a
name-6t567-yt6                   fail  red   n/a

I want to extract data from the first 2 columns and would ideally store it in a dictionary in the following manner:

[{'type': 'name1', 'status': 'pass'}, {'type': 'name-6t56-yt6', 'status': 'fail'}, {'type': 'name-45', 'status': 'pass'}, {'type': 'name-6t567-yt6', 'status': 'fail'}]

Any ideas of how to approach this?

Note that this is a multi-line string(in utf-8 format).

Upvotes: 3

Views: 383

Answers (4)

Gustav Rasmussen
Gustav Rasmussen

Reputation: 3961

With the text input defined as a multinine string, text, you can read it's content into the desired dictionary structure like this:

# from collections import defaultdict
from pprint import pprint as pp

text = """name1                            pass  blue  n/a
name-6t56-yt6                    fail  red   n/a
name-45                          pass  blue  n/a
name-6t567-yt6                   fail  red   n/a"""

d = []
for line in text.split("\n"):
    type, status = line.split()[0:2]
    d.append({"type": type, "status": status})

pp(d)

Which will output:

[{'status': 'name1', 'type': 'pass'},
 {'status': 'name-6t56-yt6', 'type': 'fail'},
 {'status': 'name-45', 'type': 'pass'},
 {'status': 'name-6t567-yt6', 'type': 'fail'}]

Upvotes: 0

timgeb
timgeb

Reputation: 78750

Assuming you want a list:

Setup:

>>> s = '''name1                            pass  blue  n/a
... name-6t56-yt6                    fail  red   n/a
... name-45                          pass  blue  n/a
... name-6t567-yt6                   fail  red   n/a'''

Construct result:

>>> [dict(zip(('type', 'status'), line.split(maxsplit=2)[:2])) for line in s.splitlines()]
[{'type': 'name1', 'status': 'pass'}, {'type': 'name-6t56-yt6', 'status': 'fail'}, {'type': 'name-45', 'status': 'pass'}, {'type': 'name-6t567-yt6', 'status': 'fail'}]

Upvotes: 2

Viewed
Viewed

Reputation: 1413

from pprint import pprint

with open('file.txt') as f:
    data = f.readlines()

result = []
for line in data:
    result.append({
        'type': line[0:line.index(' ')],
        'status': 'pass' if 'pass' in line else 'fail'
    })

pprint(result)
# [{'status': 'pass', 'type': 'name1'},
#  {'status': 'fail', 'type': 'name-6t56-yt6'},
#  {'status': 'pass', 'type': 'name-45'},
#  {'status': 'fail', 'type': 'name-6t567-yt6'}]

Upvotes: 0

Hoxha Alban
Hoxha Alban

Reputation: 1137

In your code you are using a set of dictionaries, it's not the best idea, here i am using a list of dictionaries

s = """name1                            pass  blue  n/a
name-6t56-yt6                    fail  red   n/a
name-45                          pass  blue  n/a
name-6t567-yt6                   fail  red   n/a"""

d = []
for line in s.split('\n'):
    type, status = line.split()[0:2]
    d.append({'type': type, 'status': status})

content of d:

[{'type': 'name1', 'status': 'pass'},
 {'type': 'name-6t56-yt6', 'status': 'fail'},
 {'type': 'name-45', 'status': 'pass'},
 {'type': 'name-6t567-yt6', 'status': 'fail'}]

Upvotes: 0

Related Questions