Skip to content Skip to sidebar Skip to footer

Regex To Split %ages And Values In Python

Hello I am new to python and regex. I have a large CSV file which has a field like, %age composition that contains values such as: '34% passed 23% failed 46% deferred' How would yo

Solution 1:

And if you still want to go with regular expressions, you can use this one:

(\w+)%\s(\w+)

Which would match one or more alphanumeric characters (alternative: [0-9a-zA-Z_]+) followed by % sign, space character and one or more alphanumeric characters. Parenthesis help with capturing appropriate set of characters.

Demo:

>>> import re
>>> s = '34% passed 23% failed 46% deferred'
>>> pattern = re.compile(r'(\w+)%\s(\w+)')
>>> {value: key for key, value in pattern.findall(s)}
{'failed': '23', 'passed': '34', 'deferred': '46'}

Solution 2:

You don't need to use regular expression:

>>> s = '34% passed 23% failed 46% deferred'
>>> groups = zip(*[iter(s.split())]*2)
>>> groups
[('34%', 'passed'), ('23%', 'failed'), ('46%', 'deferred')]
>>> {result: int(percent.rstrip('%')) for percent, result in groups}
{'failed': 23, 'passed': 34, 'deferred': 46}

zip(*[iter(..)]*2) came from grouper - itertools recipes (Also see How does zip(*[iter(s)]*n) work in Python?):

def grouper(iterable, n, fillvalue=None):
    "Collect data into fixed-length chunks or blocks"
    # grouper('ABCDEFG', 3, 'x') --> ABC DEF Gxx
    args = [iter(iterable)] * n
    return izip_longest(fillvalue=fillvalue, *args)

Solution 3:

Try this:

[EDIT: Added list support for words to check based on OPs request. Also cleaned the dictionary building code that alecx uses here: https://stackoverflow.com/a/25628562/3646530]

import re

data = """34% passed 23% failed 46% deferred 34% checked"""
checkList = ['passed', 'failed', 'deferred', 'checked']
result = {k:v for (v, k) in re.findall('(\d{1,3})% (' + '|'.join(checkList) + ')', data)}
print(result) # Python 3
#print result # Python 2.7

Here the regex is \d{1,3} - to catch the percentage int and passed|failed|deferred to get the type. I use a list comprehension to generate a list of tuples of keys and values, which I then convert to a dictionary

In order to build the string 'passed|failed| ..' I use the .join function of a string to join words from a checkList with a pipe character as the separator.


Post a Comment for "Regex To Split %ages And Values In Python"