如何使material_percent得到的是字符串
时间: 2024-10-27 20:14:37 浏览: 21
在当前代码中,`material_percent` 是一个字典,其值通常是列表形式(例如 `[...]`)。如果你想将 `material_percent` 中的所有值转换为字符串,可以在写入文件之前进行相应的处理。
以下是如何修改代码以确保 `material_percent` 值是字符串:
1. **在 `ClassfyMaterials` 函数中处理 `material_percent`:**
在 `ClassfyMaterials` 函数中,添加一段代码来确保 `material_percent` 的值是字符串。可以使用 `join` 方法将列表转换为字符串,并用逗号分隔每个元素。
```python
def ClassfyMaterials(material_quantity, material_unit, material_word, material_percent):
reactionMaterials = {}
type_material_quantitys = {}
words = []
uncategorizes = []
# 确保 material_percent 值是字符串
for material, percent in material_percent.items():
if isinstance(percent, list):
material_percent[material] = ','.join(map(str, percent))
# 其他代码保持不变
material_quantity[''] = ''
material_unit[''] = ''
material_percent[''] = ''
for wordList in material_word.values():
words += wordList
for type, words in material_word.items():
type_material_quantitys[type] = {}
for material, quantity in material_quantity.items():
if quantity == '':
quantity0 = 0
else:
quantity0 = deal_with_quantity(quantity)
categorized = False
for type, words in material_word.items():
if material in words:
categorized = True
type_material_quantitys[type][material] = quantity0
if not categorized:
if material:
uncategorizes.append([material, quantity, material_unit[material], material_percent[material]])
for type in material_word:
type_material = type_material_quantitys[type]
sortedMaterials = sorted(type_material.items(), key=lambda kv: (kv[1], kv[0]))
sortedMaterials.reverse()
if len(sortedMaterials) >= 3:
material1 = sortedMaterials[0][0].replace(',', '')
material2 = sortedMaterials[1][0].replace(',', '')
material3 = sortedMaterials[2][0].replace(',', '')
elif len(sortedMaterials) == 2:
material1 = sortedMaterials[0][0].replace(',', '')
material2 = sortedMaterials[1][0].replace(',', '')
material3 = ''
elif len(sortedMaterials) == 1:
material1 = sortedMaterials[0][0].replace(',', '')
material2 = ''
material3 = ''
else:
material1 = ''
material2 = ''
material3 = ''
reactionMaterials[type + '_name1'] = material1
reactionMaterials[type + '_quantity1'] = material_quantity[material1]
reactionMaterials[type + '_unit1'] = material_unit[material1]
reactionMaterials[type + '_percent1'] = material_percent.get(material1, '')
reactionMaterials[type + '_name2'] = material2
reactionMaterials[type + '_quantity2'] = material_quantity[material2]
reactionMaterials[type + '_unit2'] = material_unit[material2]
reactionMaterials[type + '_percent2'] = material_percent.get(material2, '')
reactionMaterials[type + '_name3'] = material3
reactionMaterials[type + '_quantity3'] = material_quantity[material3]
reactionMaterials[type + '_unit3'] = material_unit[material3]
reactionMaterials[type + '_percent3'] = material_percent.get(material3, '')
return reactionMaterials
```
2. **在写入文件时处理 `material_percent`:**
在写入文件时,确保 `material_percent` 的值已经转换为字符串。
```python
if __name__ == '__main__':
# 其他代码保持不变
for filepath in xmlFilepaths:
try:
uncategorizes = []
filename = GetDOIFilename(filepath)
with open(filepath, 'r', encoding='utf-8-sig') as file:
content = file.read()
content = modified_xml(content)
pH = ExtractSolutionpH(content)
product = ExtractProduct(content)
material_quantity, material_unit, material_percent = ExtractMaterials(content)
material_ratio1 = ExtractMaterialRatios1(content)
material_ratio2 = ExtractMaterialRatios2(content)
reactionMaterials = ClassfyMaterials(material_quantity, material_unit, material_word, material_percent)
if len(material_ratio1) >= len(material_ratio2):
reactionMaterialRatios = ClassfyMaterialsRatio(material_ratio1, materialRadio_word)
else:
reactionMaterialRatios = ClassfyMaterialsRatio(material_ratio2, materialRadio_word)
actionPhraseType_condition = ExtractCondition(content)
actionPhraseType_conditionPL = ExtractConditionPL(content)
actionPhraseType_conditionChecks = ExtractConditionCheck(content)
actionPhraseType_conditionCheck1 = []
list_condition = [str(x) for x in actionPhraseType_condition.values()]
list_conditionPL = [str(x) for x in actionPhraseType_conditionPL]
try:
for condition in actionPhraseType_conditionChecks:
condition = str(condition)
if condition not in list_condition and condition not in list_conditionPL:
actionPhraseType_conditionCheck1.append(condition)
actionPhraseType_conditionChecks = actionPhraseType_conditionCheck1
except:
pass
outFile.write(filename)
for type in material_word:
outFile.write(',%s,%s,%s,%s' % (
reactionMaterials[type + '_name1'].replace(',', ';'),
reactionMaterials[type + '_quantity1'],
reactionMaterials[type + '_unit1'],
reactionMaterials[type + '_percent1']
))
outFile.write(',%s,%s,%s,%s' % (
reactionMaterials[type + '_name2'].replace(',', ';'),
reactionMaterials[type + '_quantity2'],
reactionMaterials[type + '_unit2'],
reactionMaterials[type + '_percent2']
))
outFile.write(',%s,%s,%s,%s' % (
reactionMaterials[type + '_name3'].replace(',', ';'),
reactionMaterials[type + '_quantity3'],
reactionMaterials[type + '_unit3'],
reactionMaterials[type + '_percent3']
))
for type in materialRadio_word:
try:
outFile.write(',%s,%s' % (
reactionMaterialRatios['R_' + type + '_name1'].replace(',', ';'),
reactionMaterialRatios['R_' + type + '_quantity1']
))
outFile.write(',%s,%s' % (
reactionMaterialRatios['R_' + type + '_name2'].replace(',', ';'),
reactionMaterialRatios['R_' + type + '_quantity2']
))
outFile.write(',%s,%s' % (
reactionMaterialRatios['R_' + type + '_name3'].replace(',', ';'),
reactionMaterialRatios['R_' + type + '_quantity3']
))
except:
print(type)
for type in condition_word:
if type in actionPhraseType_condition:
condition = actionPhraseType_condition[type]
outFile.write(',%s,%s,%s,%s' % (condition[0], condition[1], condition[2], condition[3]))
else:
outFile.write(',%s,%s,%s,%s' % ('', '', '', ''))
for condition in actionPhraseType_conditionPL:
outFile.write(',%s,%s,%s,%s' % (condition[0], condition[1], condition[2], condition[3]))
check_condition = actionPhraseType_conditionChecks
outFile.write(',' + str(check_condition).replace(',', ';') + ',' + str(pH) + ',' + product.replace(',', ';') + ',' + str(uncategorizes).replace(',', ';') + '')
except Exception as e:
print(traceback.print_exc())
print(filename)
```
通过以上修改,`material_percent` 的值将在写入文件前被转换为字符串。这样可以确保输出文件中的 `material_percent` 列不会出现列表形式的数据。
阅读全文