[英]Getting KeyError while appending kwargs to list python
我有一個函數,可以在文本文件的每個段落中搜索關系。 如果找到,它返回找到的與段落的關系並搜索該段落中是否存在任何顏色。 搜索段落關系的 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)))
對於少數文件,我收到錯誤“
File "<ipython-input-56-f725b589f198>", line 63, in extractor
result.append(text_const.format(**kwargs).replace('\n', '').replace('\t', ''))
KeyError: 'colour'
關於如何解決這個問題的任何想法。
您不能使用.get()
將字典鍵分配給值:
kwargs.get['colour'] = wd
應該
kwargs['colour'] = wd
編輯以下評論:
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
應該:
text_const = TEXT_WITHOUT_COLOUR
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
text_const = TEXT_WITH_COLOUR
break
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.