我需要编写一个小型解析器,该解析器将从表单中提取数据。
数据将始终以一致的方式发布。如下:
Panakamanana
104412=Trident of Corrupted Waters
104411=Immerseus' Crystalline Eye
104435=Stonetoe's Tormented Treads
104455=Reality Ripper Ring
99716=Chest of the Cursed Protector
104509=Laser Burn Bracers
104531=Haromm's Talisman
99722=Gauntlets of the Cursed Protector
104562=Ring of Restless Energy
104606=Gleaming Eye of the Devilsaur
99725=Helm of the Cursed Protector
99719=Shoulders of the Cursed Protector
104616=Ticking Ebon Detonator
105686=Hellscream's Pig Sticker
我唯一感兴趣的数据是=号之前的每个整数。然后,我希望能够遍历这些内容,以便将它们放入字典或数组中,否则效果会很好。
这是完成它的一种方法:
with open('somefile.txt') as f:
next(f) # Skips the first line, which doesn't have =
numbers = [line.split('=')[0] for line in f if len(line.strip())]
print(numbers)
如果要使用正则表达式:
>>> import re
>>> s = "104412=Trident of Corrupted Waters"
>>> re.findall(r'^(\d+)', s)[0]
'104412'
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句