0

I need to write a small parser that will extract data from a form.

The data will be posted in a consistent pattern all the time. As follows:

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

The only data I'm interested in is each integer before the = sign. I want to then be able to iterate over these so perhaps putting them in a dict or array or something would be great.

1
  • 1
    Split on = perhaps would be easier? Commented Mar 22, 2014 at 12:13

3 Answers 3

2

Here is one way to get it done:

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)

If you want to use regular expressions:

>>> import re
>>> s = "104412=Trident of Corrupted Waters"
>>> re.findall(r'^(\d+)', s)[0]
'104412'
Sign up to request clarification or add additional context in comments.

Comments

1

Just split the string up using '=' as the delimiter. The most Pythonic way to implement this would be to use a list comprehension:

>>> [int(line.split('=')[0]) for line in your_lines[1:]]
[104412, 104411, ..., 105686]

Where your_lines is a list of the lines demonstrated in your question.

Comments

0

You can simply do that as follows:

new_list = [int(line[:line.find('=')]) for line in your_list]

print new_list

5 Comments

You don't need new_list = []
@Burhan, But don't I need to initialize a list in which to add the data
@BurhanKhalid, Just like that?
@sshashank124 No, that would return a NameError. He means a list comprehension. Take a look at our answers and you will see what he means.
You can do new_list = [int(line[:line.find('=')]) for line in your_list] and get rid of the for loop entirely.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.