我有一个文本文件,其中包含要转换为实际元组的元组的字符串表示形式,然后将它们作为行添加到csv文件中。但是,某些值是NULL,使用eval()会导致名称错误
文本文件中一行的示例是:
(2,'Spawn Point (Only GM can see it)',NULL,1,1,262,0,0,0,35,35,0,0,1,3,3,0,0,32768,0,0,0,0,1.14286,20,0,0,0,0,1,0,1,1,1,1,1,1,42,42,0,0,11,11,387.6,532.95,15,5,100,1800,1900,0,222,1110,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,'','')
可以看出,第三个值是NULL
这是我的代码:
import csv
filename = "D:\\Downloads\\Quest addon\\creature_template.txt"
file = open(filename, "r")
with open("D:\\Downloads\\Quest addon\\creature_template.csv", "w") as test_file:
file_writer = csv.writer(test_file, lineterminator = '\n')
file_writer.writerow(['Entry', 'Name', 'SubName', 'MinLevel', 'MaxLevel', 'ModelId1', 'ModelId2', 'ModelId3', 'ModelId4', 'FactionAlliance', 'FactionHorde', 'Scale', 'Family', 'CreatureType', 'InhabitType', 'RegenerateStats', 'RacialLeader', 'NpcFlags', 'UnitFlags', 'DynamicFlags', 'ExtraFlags', 'CreatureTypeFlags', 'SpeedWalk', 'SpeedRun', 'Detection', 'CallForHelp', 'Pursuit', 'Leash', 'Timeout', 'UnitClass', 'Rank', 'HealthMultiplier', 'PowerMultiplier', 'DamageMultiplier', 'DamageVariance', 'ArmorMultiplier', 'ExperienceMultiplier', 'MinLevelHealth', 'MaxLevelHealth', 'MinLevelMana', 'MaxLevelMana', 'MinMeleeDmg', 'MaxMeleeDmg', 'MinRangedDmg', 'MaxRangedDmg', 'Armor', 'MeleeAttackPower', 'RangedAttackPower', 'MeleeBaseAttackTime', 'RangedBaseAttackTime', 'DamageSchool', 'MinLootGold', 'MaxLootGold', 'LootId', 'PickpocketLootId', 'SkinningLootId', 'KillCredit1', 'KillCredit2', 'MechanicImmuneMask', 'SchoolImmuneMask', 'ResistanceHoly', 'ResistanceFire', 'ResistanceNature', 'ResistanceFrost', 'ResistanceShadow', 'ResistanceArcane', 'PetSpellDataId', 'MovementType', 'TrainerType', 'TrainerSpell', 'TrainerClass', 'TrainerRace', 'TrainerTemplateId', 'VendorTemplateId', 'GossipMenuId', 'EquipmentTemplateId', 'Civilian', 'AIName', 'ScriptName'])
for line in file:
line = eval(line)
file_writer.writerow(line)
运行时出现以下错误:
Traceback (most recent call last):
File "D:\Downloads\Quest addon\data to csv.py", line 10, in <module>
line = eval(line)
File "<string>", line 1, in <module>
NameError: name 'NULL' is not defined
答案 0 :(得分:0)
问题是“ NULL”在Python中不是有效的关键字。您可以使用Python关键字“ None”替换所有“ NULL”实例。也就是说,在评估行之前,您可以编写line.replace("NULL", "None")
。
答案 1 :(得分:0)
如果您执行NULL = None,则更快
import csv
filename = "D:\\Downloads\\Quest addon\\creature_template.txt"
file = open(filename, "r")
with open("D:\\Downloads\\Quest addon\\creature_template.csv", "w") as test_file:
file_writer = csv.writer(test_file, lineterminator = '\n')
file_writer.writerow(['Entry', 'Name', 'SubName', 'MinLevel', 'MaxLevel', 'ModelId1', 'ModelId2', 'ModelId3', 'ModelId4', 'FactionAlliance', 'FactionHorde', 'Scale', 'Family', 'CreatureType', 'InhabitType', 'RegenerateStats', 'RacialLeader', 'NpcFlags', 'UnitFlags', 'DynamicFlags', 'ExtraFlags', 'CreatureTypeFlags', 'SpeedWalk', 'SpeedRun', 'Detection', 'CallForHelp', 'Pursuit', 'Leash', 'Timeout', 'UnitClass', 'Rank', 'HealthMultiplier', 'PowerMultiplier', 'DamageMultiplier', 'DamageVariance', 'ArmorMultiplier', 'ExperienceMultiplier', 'MinLevelHealth', 'MaxLevelHealth', 'MinLevelMana', 'MaxLevelMana', 'MinMeleeDmg', 'MaxMeleeDmg', 'MinRangedDmg', 'MaxRangedDmg', 'Armor', 'MeleeAttackPower', 'RangedAttackPower', 'MeleeBaseAttackTime', 'RangedBaseAttackTime', 'DamageSchool', 'MinLootGold', 'MaxLootGold', 'LootId', 'PickpocketLootId', 'SkinningLootId', 'KillCredit1', 'KillCredit2', 'MechanicImmuneMask', 'SchoolImmuneMask', 'ResistanceHoly', 'ResistanceFire', 'ResistanceNature', 'ResistanceFrost', 'ResistanceShadow', 'ResistanceArcane', 'PetSpellDataId', 'MovementType', 'TrainerType', 'TrainerSpell', 'TrainerClass', 'TrainerRace', 'TrainerTemplateId', 'VendorTemplateId', 'GossipMenuId', 'EquipmentTemplateId', 'Civilian', 'AIName', 'ScriptName'])
NULL=None #added line here
for line in file:
line = eval(line)
file_writer.writerow(line)