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:
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'}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With