python plain text regex parsing

Llanilek

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.

Burhan Khalid

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'

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Parsing plain text with section in Python

From Dev

Parsing plain text with section in Python

From Dev

Python - Parsing JSON formatted text file with regex

From Dev

parsing data out of formatted plain-text

From Dev

Parsing text to object with regex

From Dev

password Regex not matching if spaces around in plain text

From Dev

ENML to plain text converter for Python

From Dev

Parsing XML in Python with regex

From Dev

Python Regex for parsing site

From Dev

Parsing XML in Python with regex

From Dev

Python Regex for parsing site

From Dev

Python - regex parsing file

From Dev

Python regex parsing of syslog

From Dev

parsing html text with regex in javascript?

From Dev

Parsing large text file with regex

From Dev

Parsing plain text to js array - add separator to the second element

From Dev

Parsing Javascript text in Python

From Dev

parsing a path based on regex - Python

From Dev

Python regex: parsing newick format

From Dev

Parsing equations in Python using regex

From Dev

Extracting plain text from HTML using Python

From Dev

Python - Extract the body from a mail in plain text

From Dev

Extracting plain text from HTML using Python

From Dev

python 3.5 imaplib read gmails as plain text

From Dev

Python: parsing only text from HTML using bs4 and RegEx

From Dev

Regex parsing text and get relevant words / characters

From Dev

Parsing text from a table of contents using regex

From Dev

Convert plain text email to clickable link - REGEX/jQuery

From Dev

How to display regex as plain text instead of resolving it PHP