Skip to content Skip to sidebar Skip to footer

Parsing Text File To Tabular Data For Processing

The problem at hand is to parse a particular data in a tabular form using python.A small part of data is shown below Statistics indicator:0x222235 number of records = 3 record

Solution 1:

Here, maybe we don't want to use regular expressions. However, if we might want to do so, we can set attribute names as a left boundary, and collect our desired digits, maybe with an expression similar to:

value\s+(one|two|three)\s+=\s+([0-9]+)

Then, the rest of our problem can be scripted. We can also add more boundaries to our expressions, if that'd be necessary.

Demo

Test

# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility

import re

regex = r"value\s+(one|two|three)\s+=\s+([0-9]+)"

test_str = ("Statistics indicator:0x222235\n\n"
    "number of records = 3\n\n"
    "records[0]\n\n"
    "value one = 2\n\n"
    "value two = 5\n\n"
    "blocks = 2\n\n"
    "block[0] {\n\n"
    "some irrelevant data....\n\n"
    "value three = 4 bytes\n\n"
    "}\n\n"
    "block[1]{\n\n"
    "some irrelevant data...\n\n"
    "value three = 6 bytes\n\n"
    "}\n\n"
    "records[1]\n\n"
    "value one = 3\n\n"
    "value two = 5\n\n"
    "blocks = 1\n\n"
    "block[0] {\n\n"
    "some irrelevant data....\n\n"
    "value three = 4 bytes\n\n"
    "}\n\n"
    "records[2]\n\n"
    "value one = 7\n\n"
    "value two = 6\n\n"
    "blocks = 2\n\n"
    "block[0] {\n\n"
    "some irrelevant data....\n\n"
    "value three = 3 bytes\n\n"
    "}\n\n"
    "block[1]{\n\n"
    "some irrelevant data...\n\n"
    "value three = 4 bytes\n\n"
    "}\n\n"
    "Statistics indicator:0x135256\n\n"
    "number of records = 2\n\n"
    "records[0]\n\n"
    "value one = 4\n\n"
    "value two = 8\n\n"
    "blocks = 1\n\n"
    "block[0] {\n\n"
    "some irrelevant data....\n\n"
    "value three = 6 bytes\n\n"
    "}\n\n"
    "records[1]\n\n"
    "value one = 3\n\n"
    "value two = 5\n\n"
    "blocks = 1\n\n"
    "block[0] {\n\n"
    "some irrelevant data....\n\n"
    "value three = 3 bytes\n\n"
    "}")

matches = re.finditer(regex, test_str, re.MULTILINE)

for matchNum, match in enumerate(matches, start=1):

    print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))

    for groupNum in range(0, len(match.groups())):
        groupNum = groupNum + 1

        print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum)))

# Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.

Demo

const regex = /value\s+(one|two|three)\s+=\s+([0-9]+)/gm;
const str = `Statistics indicator:0x222235

number of records = 3

records[0]

value one = 2

value two = 5

blocks = 2

block[0] {

some irrelevant data....

value three = 4 bytes

}

block[1]{

some irrelevant data...

value three = 6 bytes

}

records[1]

value one = 3

value two = 5

blocks = 1

block[0] {

some irrelevant data....

value three = 4 bytes

}

records[2]

value one = 7

value two = 6

blocks = 2

block[0] {

some irrelevant data....

value three = 3 bytes

}

block[1]{

some irrelevant data...

value three = 4 bytes

}

Statistics indicator:0x135256

number of records = 2

records[0]

value one = 4

value two = 8

blocks = 1

block[0] {

some irrelevant data....

value three = 6 bytes

}

records[1]

value one = 3

value two = 5

blocks = 1

block[0] {

some irrelevant data....

value three = 3 bytes

}`;
let m;

while ((m = regex.exec(str)) !== null) {
    // This is necessary to avoid infinite loops with zero-width matches
    if (m.index === regex.lastIndex) {
        regex.lastIndex++;
    }
    
    // The result can be accessed through the `m`-variable.
    m.forEach((match, groupIndex) => {
        console.log(`Found match, group ${groupIndex}: ${match}`);
    });
}

RegEx

If this expression wasn't desired, it can be modified or changed in regex101.com.

RegEx Circuit

jex.im visualizes regular expressions:

enter image description here


Post a Comment for "Parsing Text File To Tabular Data For Processing"