Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert this text file into a dictionary?

I have a file f that looks something like:

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

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

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

I have a number of sub-questions:

  1. How can I make use of the # character in order to know when a new entry is in place?
  2. How to remove it and keep whatever follows until the end of the line?
  3. How is it possible to append every string that follows on that new line until # pops up again.
  4. How can I stop when the file finishes?
like image 749
nyw Avatar asked Dec 05 '22 15:12

nyw


2 Answers

Firstly, mydict contains the keys which starts with #, and the value is a list(list can keep the lines in their appending order), we append lines into this list until we find next line that starts with #. Then we just need to convert the list of lines into one single string.

I am using python3, if you use python2 replace mydict.items() with mydict.iteritems() for iterating key-value pairs

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)

Output:

{'labelA': 'thereissomethinghere', 'label_Bbb': 'hereaswell'}
like image 50
Haifeng Zhang Avatar answered Jan 01 '23 19:01

Haifeng Zhang


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
like image 44
llulai Avatar answered Jan 01 '23 18:01

llulai