Python 如何从文本创建词典?

Python 如何从文本创建词典?,python,dictionary,Python,Dictionary,我是一名python初学者,我有几个长文本格式为列表,我想编写一个函数,提取重要信息并返回字典。文本的格式如下: ['text', 'text', 'text', 'text', 'text','text', 'text', 'Country Code', '11111', 'Country Location', 'North', 'Date', '18-03-1878', text','text','text', 'Population': '1289028', 'text', 'text',

我是一名python初学者,我有几个长文本格式为列表,我想编写一个函数,提取重要信息并返回字典。文本的格式如下:

['text', 'text', 'text', 'text', 'text','text', 'text', 'Country Code', '11111', 'Country Location', 'North', 'Date', '18-03-1878', text','text','text', 'Population': '1289028', 'text', 'text', 'Government', 'Monarchy', 'text', 'text', 'Religion:', 'Catholic']
{"Country Code": "11111", 
 "Country Location": "North", 
 "Date": "18-03-1878"
 "Population": "1289028"  
 "Religion:" "Catholic"}
我需要具体信息,如国家位置、国家代码和日期。问题是,这些字符串的位置因文本而异,因此我需要一个函数,首先在文本中查找这些信息,将其作为字典的键,并将文本上的下一个元素作为值。我希望得到这样的输出:

['text', 'text', 'text', 'text', 'text','text', 'text', 'Country Code', '11111', 'Country Location', 'North', 'Date', '18-03-1878', text','text','text', 'Population': '1289028', 'text', 'text', 'Government', 'Monarchy', 'text', 'text', 'Religion:', 'Catholic']
{"Country Code": "11111", 
 "Country Location": "North", 
 "Date": "18-03-1878"
 "Population": "1289028"  
 "Religion:" "Catholic"}

我真的很感谢你们能提供的任何帮助。

如果你不关心效率,而且关键点是一致的,你可以编写一个循环

your_list = ['text', 'text', 'text', 'text', 'text','text', 'text', 'Country Code', '11111', 'Country Location', 'North', 'Date', '18-03-1878', 'text','text','text', 'Population', '1289028', 'text', 'text', 'Government', 'Monarchy', 'text', 'text', 'Religion:', 'Catholic']

our_dict = {}

for idx, word in enumerate(your_list):
    if 'Country Code' in word:
        our_dict['Country Code'] = your_list[idx+1]
    if 'Country Location' in word:
        our_dict['Country Location'] = your_list[idx+1]
    if 'Date' in word:
        our_dict['Date'] = your_list[idx+1]
    if 'Population' in word:
        our_dict['Population'] = your_list[idx+1]
    if 'Religion' in word:
        our_dict['Religion'] = your_list[idx+1]
要处理列表中的其他空单元格问题,您可以执行以下操作:

for idx, word in enumerate(your_list):
    if len(word.strip(' ')) > 0:
        if 'Country Code' in word:
            our_dict['Country Code'] = your_list[idx+1]
        if 'Country Location' in word:
            our_dict['Country Location'] = your_list[idx+1]
        if 'Date' in word:
            our_dict['Date'] = your_list[idx+1]
        if 'Population' in word:
            our_dict['Population'] = your_list[idx+1]
        if 'Religion' in word:
            our_dict['Religion'] = your_list[idx+1]

较短的解决方案:

#Create a list of items you are interested in (this is a set - only uniques)
itemstofind = {'Country Code', 'Country Location', 'Date', 'Population', 'Religion:'}

# use a dict comprehension to find the items and take next item in the list
# assumes there is no error in the data
d = {item:longlist[ind+1] for ind, item in enumerate(longlist) if item in itemstofind}

好的,还有一个问题:在其中一个文件中,我有空行,因此字典返回其中一个键为空。有可能解决这个问题吗?我将在代码中添加第二部分来处理空列表项。或者我很快就完成了。第二部分不起作用,因为它检查当前单元格,您必须检查下一个单元格suppose@AlexisDrakopoulos我指的是if语句。首先,您应该使用if-else子句。但是,如果找到元素,则可以将它们全部跳过。只有在元素始终在一起时,才可以跳过它们,而不是在元素之间有更多文本时