I have long text. And I’m converting this string to dict.
Here is code
data_dict = {}
filter_dict = {}
for each in text.split("\n"):
temp = each.split('=')
if len(temp) == 2:
data_dict[temp[0]] = temp[1]
data = dict((k.strip(), v.strip()) for k, v in data_dict.iteritems())
Here is output which is convert from text to dict
{'producer': 'Sailadhar Baruah',
'image': 'paporithefilm.jpg',
'distributor': '',
'alt': '',
'image size': '',
'gross': '',
'writer': 'Jahnu Barua',
'cinematography': 'Binod Pradhan',
'music': 'Satya Baruah P. P. Vidyanathan',
'followed by': '',
'narrator': '',
'director': 'Jahnu Barua',
'released': '1986',
'studio': 'Dolphin s Pvt. Ltd',
'starring': 'Gopi Desai Biju Phukan Sushil Goswami Chetana Das Dulal Roy',
'editing': '',
'name': 'Papori',
'language': 'Assamese languageAssamese',
'country': 'Assam, IND', 'budget': '',
'caption': 'A Screenshot',
'preceded by': '',
'runtime': '144 minutes'}
I just want to know where is my last paragraph gone? Can I store last paragraph text to any varible? thanks
As has been pointed out, you are only matching when you have a
key = valueformat. Try something like this instead.Here, your paragraph will be added to ‘no_key’. I started out my answer using a defaultdict from the collections module, and setting the value to be lists so you could track any unkeyed values, but, if your format is consistent, then the above should work.