要在生成的CSV文件中将数值为0的字段替换为空字符串,可以在写入每一行数据时进行判断和替换。具体来说,可以在写入每个值之前检查该值是否为0,如果是0,则将其替换为空字符串。以下是修改后的代码片段:
```python
if __name__ == '__main__':
inPath = 'F:\\AWenXianTiQu\\SomePathFile-18\\JavaResult'
dicPath = 'F:\\AWenXianTiQu\\SomePathFile-18\\dic-namecheck'
outCsvName = 'F:\\AWenXianTiQu\\SomePathFile-18\\18-01.csv'
odinaryDicFilename = dicPath + '/' + 'DicOdinary_0802.txt'
radioDicFilename = dicPath + '/' + 'DicRatio_0802.txt'
conditionDicFilename = dicPath + '/' + 'DicCondition.txt'
normalizedFilename = dicPath + '/' + '0511.txt'
material_word = ReadDicFile(odinaryDicFilename)
materialRadio_word = ReadDicFile(radioDicFilename)
condition_word = ReadDicFile(conditionDicFilename)
formula_word = ReadDicFile(normalizedFilename)
outFile = open(outCsvName, 'w', encoding='utf-8-sig')
# 写入表头
outFile.write('filename')
for type in material_word:
outFile.write(',%s,%s,%s' % (type + '_name1', type + '_quantity1', type + '_unit1'))
outFile.write(',%s,%s,%s' % (type + '_name2', type + '_quantity2', type + '_unit2'))
outFile.write(',%s,%s,%s' % (type + '_name3', type + '_quantity3', type + '_unit3'))
for type in materialRadio_word:
outFile.write(',%s,%s' % ('R_' + type + '_name1', 'R_' + type + '_quantity1'))
outFile.write(',%s,%s' % ('R_' + type + '_name2', 'R_' + type + '_quantity2'))
outFile.write(',%s,%s' % ('R_' + type + '_name3', 'R_' + type + '_quantity3'))
for type in condition_word:
outFile.write(',%s,%s,%s,%s' % (type + '_tempNumber', type + '_tempUnit', type + 'timeNumber', type + 'timeUnit'))
outFile.write(',%s,%s,%s,%s,%s,%s,%s,%s' % ('cry1_tempNumber', 'cry1_tempUnit', 'cry1_timeNumber', 'cry1_timeUnit', 'cry2_tempNumber', 'cry2_tempUnit', 'cry2_timeNumber', 'cry2_timeUnit'))
outFile.write(',check_condition,pH,product,uncategorizes')
outFile.write('\n')
xmlFilepaths = GetFilenames(inPath, 'xml')
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' % (
'' if reactionMaterials[type + '_name1'] == '0' else reactionMaterials[type + '_name1'].replace(',', ';').replace('(%', ''),
'' if reactionMaterials[type + '_quantity1'] == '0' else reactionMaterials[type + '_quantity1'],
'' if reactionMaterials[type + '_unit1'] == '0' else reactionMaterials[type + '_unit1']
))
outFile.write(',%s,%s,%s' % (
'' if reactionMaterials[type + '_name2'] == '0' else reactionMaterials[type + '_name2'].replace(',', ';').replace('(%', ''),
'' if reactionMaterials[type + '_quantity2'] == '0' else reactionMaterials[type + '_quantity2'],
'' if reactionMaterials[type + '_unit2'] == '0' else reactionMaterials[type + '_unit2']
))
outFile.write(',%s,%s,%s' % (
'' if reactionMaterials[type + '_name3'] == '0' else reactionMaterials[type + '_name3'].replace(',', ';').replace('(%', ''),
'' if reactionMaterials[type + '_quantity3'] == '0' else reactionMaterials[type + '_quantity3'],
'' if reactionMaterials[type + '_unit3'] == '0' else reactionMaterials[type + '_unit3']
))
for type in materialRadio_word:
try:
outFile.write(',%s,%s' % (
'' if reactionMaterialRatios['R_' + type + '_name1'] == '0' else reactionMaterialRatios['R_' + type + '_name1'].replace(',', ';').replace('(%', ''),
'' if reactionMaterialRatios['R_' + type + '_quantity1'] == '0' else reactionMaterialRatios['R_' + type + '_quantity1']
))
outFile.write(',%s,%s' % (
'' if reactionMaterialRatios['R_' + type + '_name2'] == '0' else reactionMaterialRatios['R_' + type + '_name2'].replace(',', ';').replace('(%', ''),
'' if reactionMaterialRatios['R_' + type + '_quantity2'] == '0' else reactionMaterialRatios['R_' + type + '_quantity2']
))
outFile.write(',%s,%s' % (
'' if reactionMaterialRatios['R_' + type + '_name3'] == '0' else reactionMaterialRatios['R_' + type + '_name3'].replace(',', ';').replace('(%', ''),
'' if reactionMaterialRatios['R_' + type + '_quantity3'] == '0' else 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' % (
'' if condition[0] == '0' else condition[0],
'' if condition[1] == '0' else condition[1],
'' if condition[2] == '0' else condition[2],
'' if condition[3] == '0' else condition[3]
))
else:
outFile.write(',%s,%s,%s,%s' % ('', '', '', ''))
for condition in actionPhraseType_conditionPL:
outFile.write(',%s,%s,%s,%s' % (
'' if condition[0] == '0' else condition[0],
'' if condition[1] == '0' else condition[1],
'' if condition[2] == '0' else condition[2],
'' if condition[3] == '0' else condition[3]
))
check_condition = actionPhraseType_conditionChecks
outFile.write(',' + str(check_condition).replace(',', ';') + ',' + str(pH) + ',' + product.replace(',', ';') + ',' + str(uncategorizes).replace(',', ';') + '\n')
except Exception as e:
print(traceback.print_exc())
print(filename)
outFile.close()
```
在这个修改后的代码中,每次写入一个值时都会检查该值是否为`0`,如果是`0`则替换为空字符串`''`。这样可以确保最终生成的CSV文件中不会出现数值为0的字段。