将kwargs追加到列表python时获取KeyError

将kwargs追加到列表python时获取KeyError,python,python-3.x,list,keyerror,keyword-argument,Python,Python 3.x,List,Keyerror,Keyword Argument,我有一个函数,可以搜索文本文件每个段落中的关系。如果找到,它将返回找到的与段落的关系,并搜索该段落中是否存在任何颜色。样品 在段落中搜索关系的metadata.csv文件如下所示: Blister Base Web PVC/PVDC Blister Foil Aluminium Blister Base Web PVC/PVDC Blister Foil Aluminium Vial Glass

我有一个函数,可以搜索文本文件每个段落中的关系。如果找到,它将返回找到的与段落的关系,并搜索该段落中是否存在任何颜色。样品 在段落中搜索关系的metadata.csv文件如下所示:

Blister     Base Web    PVC/PVDC
Blister     Foil         Aluminium
Blister     Base Web    PVC/PVDC
Blister     Foil         Aluminium
Vial        Glass       Borosilicate Glass
Vial        Stopper     Bromobutyl Rubber
Vial        Cap         Aluminium
The tablets are filled into cylindrically shaped bottles made of white coloured
polyethylene. The volumes of the bottles depend on the tablet strength and amount of
tablets, ranging from 20 to 175 ml. The screw type cap is made of white coloured
polypropylene and is equipped with a tamper proof ring.

PVC/PVDC blister pack

Blisters are made in a thermo-forming process from a PVC/PVDC base web. Each tablet
is filled into a separate blister and a lidding foil of aluminium is welded on. The blisters
are opened by pressing the tablets through the lidding foil. PVDC foil is in contact with
the tablets.

Aluminium blister pack

Blisters are made in a cold-forming process from an aluminium base web. Each tablet is
filled into a separate blister and a lidding foil of aluminium is welded on. The blisters
are opened by pressing the tablets through the lidding foil.
import csv
import re
import os
#import pdb         
def extractor(filepath):
    #pdb.set_trace()
    #pdb.set_trace()    
    TEXT_WITHOUT_COLOUR = 'Stage {counter} : Package Description: {sen} Values: {values}'
    TEXT_WITH_COLOUR = TEXT_WITHOUT_COLOUR + ','  ' Colour: {colour}'
    colours = ['White', 'Yellow', 'Blue', 'Green', 'Black', 'Brown', 'Silver', 'Purple', 'Navy blue', 'Gray', 'Orange', 'Maroon', 'pink', 'colourless', 'blue']
    counter = 1
    result = [] 
    unique_desc = [] #every unique description is stored 
    outputs      = [] 
    with open(filepath, encoding='utf-8') as f:
        data=f.read()
        paragraphs=data.split("\n\n")
    inputfile = r"C:\Users\metadata.csv"                
    inputm = []

    with open(inputfile, "r") as f:
        reader = csv.reader(f, delimiter="\t")
        for row in reader:
            #types = row.split(',')
            inputm.append(row)

    final_ref = [] 
    for lists in inputm:
        final_ref.append(str(lists[0]).split(','))
    def is_missing(words, sen):
        for w in words:
            if w.lower() not in sen.lower():
                return True
        return False



    #pdb.set_trace()
    for sen in paragraphs:
        for words in final_ref:

#            print(sen)
#            print("HHHHHHHHHHHHHHHHHHHHXXXXXXXXXXXXXX")
            if is_missing(words, sen):
                continue

            kwargs = {
                'counter': counter,
                'sen': sen,
                'values': str(words)
            }

            if (words[0] == 'Bottle') or (words[0]=='Vial') or (words[0] =='Container') or (words[0] =='Ampoules') or (words[0] =='Occlusive Dressing'):
                for wd in colours:
                    if wd.lower() in sen.lower():
                        kwargs['colour'] = wd
                        break
                text_const = TEXT_WITH_COLOUR
            else:
                text_const = TEXT_WITHOUT_COLOUR

            result.append(text_const.format(**kwargs).replace('\n', '').replace('\t', ''))

#         


            for desc in result:

                compare = re.search(r'Package Description:(.*?)Values:',desc).group(1).replace(' ','') #clean spaces

                if compare in unique_desc:  

                    group = str(unique_desc.index(compare)+1) #index starts in 0 and group in 1     
                    desc = re.sub('Stage \d','Group '+group, desc)
                    outputs.append(desc)

                else: 

                    unique_desc.append(compare)     
                    group = str(len(unique_desc))    #new group

                    desc = re.sub('Stage \d','Group '+group, desc)
                    outputs.append(desc)
                    counter+=1
                    #continue
                    #break



            #counter += 1



#    return output            
    return (sorted(set(outputs)))       
示例文本文件如下所示:

Blister     Base Web    PVC/PVDC
Blister     Foil         Aluminium
Blister     Base Web    PVC/PVDC
Blister     Foil         Aluminium
Vial        Glass       Borosilicate Glass
Vial        Stopper     Bromobutyl Rubber
Vial        Cap         Aluminium
The tablets are filled into cylindrically shaped bottles made of white coloured
polyethylene. The volumes of the bottles depend on the tablet strength and amount of
tablets, ranging from 20 to 175 ml. The screw type cap is made of white coloured
polypropylene and is equipped with a tamper proof ring.

PVC/PVDC blister pack

Blisters are made in a thermo-forming process from a PVC/PVDC base web. Each tablet
is filled into a separate blister and a lidding foil of aluminium is welded on. The blisters
are opened by pressing the tablets through the lidding foil. PVDC foil is in contact with
the tablets.

Aluminium blister pack

Blisters are made in a cold-forming process from an aluminium base web. Each tablet is
filled into a separate blister and a lidding foil of aluminium is welded on. The blisters
are opened by pressing the tablets through the lidding foil.
import csv
import re
import os
#import pdb         
def extractor(filepath):
    #pdb.set_trace()
    #pdb.set_trace()    
    TEXT_WITHOUT_COLOUR = 'Stage {counter} : Package Description: {sen} Values: {values}'
    TEXT_WITH_COLOUR = TEXT_WITHOUT_COLOUR + ','  ' Colour: {colour}'
    colours = ['White', 'Yellow', 'Blue', 'Green', 'Black', 'Brown', 'Silver', 'Purple', 'Navy blue', 'Gray', 'Orange', 'Maroon', 'pink', 'colourless', 'blue']
    counter = 1
    result = [] 
    unique_desc = [] #every unique description is stored 
    outputs      = [] 
    with open(filepath, encoding='utf-8') as f:
        data=f.read()
        paragraphs=data.split("\n\n")
    inputfile = r"C:\Users\metadata.csv"                
    inputm = []

    with open(inputfile, "r") as f:
        reader = csv.reader(f, delimiter="\t")
        for row in reader:
            #types = row.split(',')
            inputm.append(row)

    final_ref = [] 
    for lists in inputm:
        final_ref.append(str(lists[0]).split(','))
    def is_missing(words, sen):
        for w in words:
            if w.lower() not in sen.lower():
                return True
        return False



    #pdb.set_trace()
    for sen in paragraphs:
        for words in final_ref:

#            print(sen)
#            print("HHHHHHHHHHHHHHHHHHHHXXXXXXXXXXXXXX")
            if is_missing(words, sen):
                continue

            kwargs = {
                'counter': counter,
                'sen': sen,
                'values': str(words)
            }

            if (words[0] == 'Bottle') or (words[0]=='Vial') or (words[0] =='Container') or (words[0] =='Ampoules') or (words[0] =='Occlusive Dressing'):
                for wd in colours:
                    if wd.lower() in sen.lower():
                        kwargs['colour'] = wd
                        break
                text_const = TEXT_WITH_COLOUR
            else:
                text_const = TEXT_WITHOUT_COLOUR

            result.append(text_const.format(**kwargs).replace('\n', '').replace('\t', ''))

#         


            for desc in result:

                compare = re.search(r'Package Description:(.*?)Values:',desc).group(1).replace(' ','') #clean spaces

                if compare in unique_desc:  

                    group = str(unique_desc.index(compare)+1) #index starts in 0 and group in 1     
                    desc = re.sub('Stage \d','Group '+group, desc)
                    outputs.append(desc)

                else: 

                    unique_desc.append(compare)     
                    group = str(len(unique_desc))    #new group

                    desc = re.sub('Stage \d','Group '+group, desc)
                    outputs.append(desc)
                    counter+=1
                    #continue
                    #break



            #counter += 1



#    return output            
    return (sorted(set(outputs)))       
提取此信息的函数如下所示:

Blister     Base Web    PVC/PVDC
Blister     Foil         Aluminium
Blister     Base Web    PVC/PVDC
Blister     Foil         Aluminium
Vial        Glass       Borosilicate Glass
Vial        Stopper     Bromobutyl Rubber
Vial        Cap         Aluminium
The tablets are filled into cylindrically shaped bottles made of white coloured
polyethylene. The volumes of the bottles depend on the tablet strength and amount of
tablets, ranging from 20 to 175 ml. The screw type cap is made of white coloured
polypropylene and is equipped with a tamper proof ring.

PVC/PVDC blister pack

Blisters are made in a thermo-forming process from a PVC/PVDC base web. Each tablet
is filled into a separate blister and a lidding foil of aluminium is welded on. The blisters
are opened by pressing the tablets through the lidding foil. PVDC foil is in contact with
the tablets.

Aluminium blister pack

Blisters are made in a cold-forming process from an aluminium base web. Each tablet is
filled into a separate blister and a lidding foil of aluminium is welded on. The blisters
are opened by pressing the tablets through the lidding foil.
import csv
import re
import os
#import pdb         
def extractor(filepath):
    #pdb.set_trace()
    #pdb.set_trace()    
    TEXT_WITHOUT_COLOUR = 'Stage {counter} : Package Description: {sen} Values: {values}'
    TEXT_WITH_COLOUR = TEXT_WITHOUT_COLOUR + ','  ' Colour: {colour}'
    colours = ['White', 'Yellow', 'Blue', 'Green', 'Black', 'Brown', 'Silver', 'Purple', 'Navy blue', 'Gray', 'Orange', 'Maroon', 'pink', 'colourless', 'blue']
    counter = 1
    result = [] 
    unique_desc = [] #every unique description is stored 
    outputs      = [] 
    with open(filepath, encoding='utf-8') as f:
        data=f.read()
        paragraphs=data.split("\n\n")
    inputfile = r"C:\Users\metadata.csv"                
    inputm = []

    with open(inputfile, "r") as f:
        reader = csv.reader(f, delimiter="\t")
        for row in reader:
            #types = row.split(',')
            inputm.append(row)

    final_ref = [] 
    for lists in inputm:
        final_ref.append(str(lists[0]).split(','))
    def is_missing(words, sen):
        for w in words:
            if w.lower() not in sen.lower():
                return True
        return False



    #pdb.set_trace()
    for sen in paragraphs:
        for words in final_ref:

#            print(sen)
#            print("HHHHHHHHHHHHHHHHHHHHXXXXXXXXXXXXXX")
            if is_missing(words, sen):
                continue

            kwargs = {
                'counter': counter,
                'sen': sen,
                'values': str(words)
            }

            if (words[0] == 'Bottle') or (words[0]=='Vial') or (words[0] =='Container') or (words[0] =='Ampoules') or (words[0] =='Occlusive Dressing'):
                for wd in colours:
                    if wd.lower() in sen.lower():
                        kwargs['colour'] = wd
                        break
                text_const = TEXT_WITH_COLOUR
            else:
                text_const = TEXT_WITHOUT_COLOUR

            result.append(text_const.format(**kwargs).replace('\n', '').replace('\t', ''))

#         


            for desc in result:

                compare = re.search(r'Package Description:(.*?)Values:',desc).group(1).replace(' ','') #clean spaces

                if compare in unique_desc:  

                    group = str(unique_desc.index(compare)+1) #index starts in 0 and group in 1     
                    desc = re.sub('Stage \d','Group '+group, desc)
                    outputs.append(desc)

                else: 

                    unique_desc.append(compare)     
                    group = str(len(unique_desc))    #new group

                    desc = re.sub('Stage \d','Group '+group, desc)
                    outputs.append(desc)
                    counter+=1
                    #continue
                    #break



            #counter += 1



#    return output            
    return (sorted(set(outputs)))       
对于少数文件,我得到了错误“

文件“”,第63行,在提取器中
result.append(文本常量格式(**kwargs).replace('\n','').replace('\t','')
关键错误:“颜色”

您知道如何解决此问题吗。

您不能使用
将字典键分配给值。get()

kwargs.get['color']=wd
应该是

kwargs['color']=wd

编辑以下评论:

如果(词语[0]=“瓶子”)或(词语[0]=“小瓶”)或(词语[0]=“容器”)或(词语[0]=“安瓿”)或(词语[0]=“封闭敷料”):
颜色的wd:
如果sen.lower()中的wd.lower():
kwargs['color']=wd
打破
text_const=带有颜色的text_
其他:
text\u const=不带颜色的text\u
应该是:

text\u const=text\u不带颜色
如果(词语[0]=“瓶子”)或(词语[0]=“小瓶”)或(词语[0]=“容器”)或(词语[0]=“安瓿”)或(词语[0]=“封闭敷料”):
颜色的wd:
如果sen.lower()中的wd.lower():
kwargs['color']=wd
text_const=带有颜色的text_
打破

您不能使用
.get()
为值分配字典键:

kwargs.get['color']=wd
应该是

kwargs['color']=wd

编辑以下评论:

如果(词语[0]=“瓶子”)或(词语[0]=“小瓶”)或(词语[0]=“容器”)或(词语[0]=“安瓿”)或(词语[0]=“封闭敷料”):
颜色的wd:
如果sen.lower()中的wd.lower():
kwargs['color']=wd
打破
text_const=带有颜色的text_
其他:
text\u const=不带颜色的text\u
应该是:

text\u const=text\u不带颜色
如果(词语[0]=“瓶子”)或(词语[0]=“小瓶”)或(词语[0]=“容器”)或(词语[0]=“安瓿”)或(词语[0]=“封闭敷料”):
颜色的wd:
如果sen.lower()中的wd.lower():
kwargs['color']=wd
text_const=带有颜色的text_
打破

你是
text\u const
格式字符串有
{color}
但是
kwargs
不包含键
color
。你需要确保
kwargs
有键
color
或者确保
text\u const
不使用
{color}
。它是正确的。我在这一行中声明了……如果sen.lower()中的wd.lower():kwargs['color']=wd,那么当您得到错误时,您的条件
sen.lower()中的wd.lower()不匹配。打印
wd.lower()
sen.lower()
。崩溃前的打印输出将在条件不匹配时向您显示。True。它不匹配。但如何处理此类情况?您是
文本常量
格式字符串有
{color}
但是
kwargs
不包含键
颜色
。您需要确保
kwargs
有键
颜色
或者确保
文本常量
不使用
{color}
。它是正确的。我在这一行中声明了……如果sen.lower()中的wd.lower():kwargs['color']=wd,那么当您得到错误时,您的条件
sen.lower()中的wd.lower()不匹配。打印
wd.lower()
sen.lower()
。崩溃前的打印输出将在条件不匹配时向您显示。是的。它不匹配。但我如何处理此类情况?是的,实际上我只尝试过。我在粘贴代码时错误地复制了get方法。使用kwargs['color']=wd,我只得到了错误文件“,第63行,在提取器结果中。追加(文本常量格式(**kwargs)。替换('\n','')。替换('\t','')键错误:'color'Print
kwargs
,然后查看打印的内容。我唯一的想法是
text\u const.format()
函数在其操作的某个点调用
color
键,但这应该被错误堆栈捕获。在result.append line之前打印kwargs。好吧,这个错误证实了我的推测。如果未触发wd.lower()
,则代码的部分
。易于修复(无需全局)。在
if(words[0]='battle')
行之前指定不带颜色的
text\u const=text\u,然后删除
else:
部分。您将把
text\u const
分配给
text\u(不带颜色)
,并且当且仅当触发时,将
text\u const
分配给
text\u(带颜色)
。是的,实际上我只尝试过这个。我在粘贴代码时错误地复制了get方法。使用kwargs['color']=wd时,我只在提取器结果的第63行得到错误文件“”。追加(文本常量格式(**kwargs)。替换('\n','')。替换('\t','')键错误:'color'Print
kwargs
,就在该行之前,查看打印的内容。我唯一的想法是
text\u const.format()
函数在其操作的某个点调用
color
键,但这应该被错误堆栈捕获。在result.append line之前打印kwargs。好吧,这个错误证实了我的推测。如果未触发wd.lower()
,则代码的部分
。易于修复(无需全局)。将
text\u const=text\u分配给
if(单词[0]