How to convert this text file to a dictionary?

I have a file fthat looks something like this:

#labelA
there
is
something
here
#label_Bbb
here
aswell
...

It can have several labels and any number of elements (only str) per line and several lines for each label. I would like to store this data in a dictionary, for example:

d = {'labelA': 'thereissomethinghere', 'label_Bbb': 'hereaswell', ...}

I have a number of questions:

  • How can I use the # symbol to know when a new record will be in place?
  • How to delete it and save everything that follows to the end of the line?
  • How can I add each line that follows on this new line until the message # appears.
  • How can I stop when a file ends?
+4
source share
7 answers

-, mydict , #, ( ), , , #. .

python3, python2, mydict.items() mydict.iteritems() -

mydict = dict()
with open("sample.csv") as inputs:
    for line in inputs:
        if line.startswith("#"):
            key = line.strip()[1:]
            mydict.setdefault(key,list())
        else:
            mydict[key].append(line.strip())

result = dict()
for key, vlist in mydict.items():
    result[key] = "".join(vlist)

print(result)

:

{'labelA': 'thereissomethinghere', 'label_Bbb': 'hereaswell'}
+8

re.findall():

import re 

with open("lines.txt", 'r') as fh:
    d = {k:v.replace('\n', '') for k,v in re.findall(r'^#(\w+)\s([^#]+)', fh.read(), re.M)}

print(d)

:

{'label_Bbb': 'hereaswell', 'labelA': 'thereissomethinghere'}

re.findall , ,

+2
f = open('untitled.txt', 'r')

line = f.readline()
d = {}
last_key = None
last_element = ''
while line:
    if line.startswith('#'):
        if last_key:
            d[last_key] = last_element
            last_element = ''
        last_key = line[:-1]
        last_element = ''
    else:
        last_element += line
    line = f.readline()

d[last_key] = last_element
+2

collections.defaultdict:

from collections import defaultdict

d = defaultdict(list)

with open('f.txt') as file:
    for line in file:
        if line.startswith('#'):
            key = line.lstrip('#').rstrip('\n')
        else:
            d[key].append(line.rstrip('\n'))
for key in d:
    d[key] = ''.join(d[key])
+1

:

res = {}
with open("sample") as lines:
    try:
        line = lines.next()
        while True:
            entry = ""
            if line.startswith("#"):
                next = lines.next()
                while not next.startswith("#"):
                    entry += next
                    next = lines.next()
            res[line[1:]] = entry
            line = next
    except StopIteration:
        res[line[1:]] = entry  # Catch the last entry
+1

- ( , !)

dict = dict()
key = read_line()[1:]
while not end_file():
    text = ""
    line = read_line()
    while(line[0] != "#" and not end_file()):
        text += line
        line = read_line()

    dict[key] = text
    key = line[1:]
+1

:

def eachChunk(stream):
  key = None
  for line in stream:
    if line.startswith('#'):
      line = line.rstrip('\n')
      if key:
        yield key, value
      key = line[1:]
      value = ''
    else:
      value += line
  yield key, value

You can quickly create the desired dictionary as follows:

with open('f') as data:
  d = dict(eachChunk(data))
+1
source

Source: https://habr.com/ru/post/1669752/


All Articles