0

I've written some code, that analyses .txt file line by line, however it is written quite bad. Currently I have brain fog and my code has issue and my txt file needs blank line at the end in order to create proper last group. Without this line function sets name of last group the same as one before last, so that it causes further problems.

The txt file looks like this:

time in seconds
addr1,channel1
addr2,channel2
staticval1,channel3
staticval2,channel4
groupname1
addr3,channel5
staticval3,channel6
groupname2

etc


1
239.0.1.104,kanal1
233.51.52.170,kanal2
239.0.1.105,kanal3
1.0,kanal4
2.0,kanal5
grupa12
239.0.1.122,kanal6
239.0.1.116,kanal7
1.5,kanal8
4.5,kanal9
grupa123
239.0.1.119,kanal10
239.0.1.112,kanal11
4.5,KANAL112
4.0,KANAL123
1.5,KANAL134
grupa1234

and the code is:

with open("INPUT_DATA_SIMULATION.txt", "r") as filestream:
    for line in islice(filestream, 0, 1):
        scanTime = int(line)
    for line in filestream:
        currentline = line.split(",")
        if not "grupa" in line: 
            #walk thorugh lines inbetween "groups"
            if(currentline != ["\n"]):
                for y in range(len(currentline[0])):
                    if(currentline[0][y] == "."):
                        valChecker += 1
                if(valChecker>=2):
                    MCAST_ADDRESSES.append(currentline[0])
                elif(valChecker<=1):
                    MCAST_VALUES.append(float(currentline[0]))
                valChecker = 0
                if("\n" in currentline[1]):
                    currentline[1] = currentline[1][:-1]
                    MCAST_NAMES.append(currentline[1])
                else:
                    MCAST_NAMES.append(currentline[1])
            #if blank line found break loop
            else:                
                break
        elif "grupa" in line:
            groupName = line[:-1]
            shouldAdd = True
        if(shouldAdd):
            MCAST_GRP.append(groupName)
            MCAST_GRP.append(MCAST_ADDRESSES)
            MCAST_GRP.append(MCAST_VALUES)
            MCAST_GRP.append(MCAST_NAMES)

            MCAST_GROUPS.append(MCAST_GRP)

            MCAST_GRP = []
            MCAST_ADDRESSES = [] 
            MCAST_VALUES = []
            MCAST_NAMES  = []
            shouldAdd = False

return MCAST_GROUPS, scanTime

How should I modify code in order to input format be:

time in seconds
groupname1
addr1,channel1
static1,channel2
groupname2
addr2,channel3

so the group name is before addreses and static values, not after? Also modifying part responsible for deleting "\n" at the end of the lines would be great.

Thank you in advance.

1 Answers1

1

Created two versions as requested

  • get_input-handles groupname after each group
  • get_rev_input-handle groupname before each group

Code follows original flow but with some cleanup

Handle groupname after each group

from itertools import islice
import re
import pprint

def get_input(filepath):
    with open(filepath, "r") as filestream:
        MCAST_ADDRESSES = []
        MCAST_NAMES = []
        MCAST_VALUES = []
        MCAST_GRP = []
        MCAST_GROUPS = []

        # Various line patterns (capture using regex)
        mcast_pattern = re.compile(r"(^(\d{1,3}\.){3}\d{1,3}),(\w+)", re.IGNORECASE)
        number_pattern = re.compile(r"(^(^\d+\.\d+),(\w+))$", re.IGNORECASE)
        number_pattern = re.compile(r"((^\d+\.\d+),(\w+))$", re.IGNORECASE)
        group_pattern = re.compile(r"(grupa\d+)$", re.IGNORECASE)

        for line in islice(filestream, 0, 1):
            scanTime = int(line)

        for line in filestream:
            currentline = line.rstrip() # strips off ending "/n"

            search = group_pattern.search(currentline)

            if not search: 
                search = mcast_pattern.search(currentline)
                if search:
                    MCAST_ADDRESSES.append(search.group(1)) # ID
                    MCAST_NAMES.append(search.group(3))      # Name

                search = number_pattern.search(currentline)
                if search:
                    MCAST_VALUES.append(search.group(2))
                    MCAST_NAMES.append(search.group(3))         

            else:
                groupName = search.group(1)
                MCAST_GRP.append(groupName)
                MCAST_GRP.append(MCAST_ADDRESSES)
                MCAST_GRP.append(MCAST_VALUES)
                MCAST_GRP.append(MCAST_NAMES)

                MCAST_GROUPS.append(MCAST_GRP)

                MCAST_GRP = []
                MCAST_ADDRESSES = [] 
                MCAST_VALUES = []
                MCAST_NAMES  = []


    return MCAST_GROUPS, scanTime

Usage

data, scanTime = get_input(r".\input_simulation\INPUT_DATA_SIMULATION.txt")

print("Scan time: {}".format(scanTime))
pp = pprint.PrettyPrinter(indent=4)
pp.pprint(data)

Output

Scan time: 1
[   [   'grupa12',
        ['239.0.1.104', '233.51.52.170', '239.0.1.105'],
        ['1.0', '2.0'],
        ['kanal1', 'kanal2', 'kanal3', 'kanal4', 'kanal5']],
    [   'grupa123',
        ['239.0.1.122', '239.0.1.116'],
        ['1.5', '4.5'],
        ['kanal6', 'kanal7', 'kanal8', 'kanal9']],
    [   'grupa1234',
        ['239.0.1.119', '239.0.1.112'],
        ['4.5', '4.0', '1.5'],
        ['kanal10', 'kanal11', 'KANAL112', 'KANAL123', 'KANAL134']]]

Handle groupname before group

from itertools import islice
import re
import pprint

def get_rev_input(filepath):
    with open(filepath, "r") as filestream:
        MCAST_ADDRESSES = []
        MCAST_NAMES = []
        MCAST_VALUES = []
        MCAST_GRP = []
        MCAST_GROUPS = []

        # Various line patterns (capture using regex)
        mcast_pattern = re.compile(r"(^(\d{1,3}\.){3}\d{1,3}),(\w+)", re.IGNORECASE)
        number_pattern = re.compile(r"((^\d+\.\d+),(\w+))$", re.IGNORECASE)
        group_pattern = re.compile(r"(grupa\d+)$", re.IGNORECASE)

        for line in islice(filestream, 0, 1):
            scanTime = int(line)

        groupName = ""
        for line in filestream:
            currentline = line.rstrip() # strips off ending "/n"

            search = group_pattern.search(currentline)

            if not search: 
                # Not groupname
                search = mcast_pattern.search(currentline)
                if search:
                    MCAST_ADDRESSES.append(search.group(1)) # ID
                    MCAST_NAMES.append(search.group(3))      # Name

                search = number_pattern.search(currentline)
                if search:
                    MCAST_VALUES.append(search.group(2))
                    MCAST_NAMES.append(search.group(3))         

            else:
                if not groupName:
                    groupName = search.group(1)
                    continue

                MCAST_GRP.append(groupName)
                MCAST_GRP.append(MCAST_ADDRESSES)
                MCAST_GRP.append(MCAST_VALUES)
                MCAST_GRP.append(MCAST_NAMES)
                MCAST_GROUPS.append(MCAST_GRP)

                MCAST_GRP = []
                MCAST_ADDRESSES = [] 
                MCAST_VALUES = []
                MCAST_NAMES  = []

                # Update group name
                groupName = search.group(1)


        # Capture Last Group
        if groupName:
            MCAST_GRP.append(groupName)
            MCAST_GRP.append(MCAST_ADDRESSES)
            MCAST_GRP.append(MCAST_VALUES)
            MCAST_GRP.append(MCAST_NAMES)
            MCAST_GROUPS.append(MCAST_GRP)

    return MCAST_GROUPS, scanTime

Usage

data, scanTime = get_rev_input(r".\input_simulation\revINPUT_DATA_SIMULATION.txt")

print("Scan time: {}".format(scanTime))
pp = pprint.PrettyPrinter(indent=4)
pp.pprint(data)

Output

Scan time: 1
[   [   'grupa12',
        ['239.0.1.104', '233.51.52.170', '239.0.1.105'],
        ['1.0', '2.0'],
        ['kanal1', 'kanal2', 'kanal3', 'kanal4', 'kanal5']],
    [   'grupa123',
        ['239.0.1.122', '239.0.1.116'],
        ['1.5', '4.5'],
        ['kanal6', 'kanal7', 'kanal8', 'kanal9']],
    [   'grupa1234',
        ['239.0.1.119', '239.0.1.112'],
        ['4.5', '4.0', '1.5'],
        ['kanal10', 'kanal11', 'KANAL112', 'KANAL123', 'KANAL134']]]
DarrylG
  • 16,732
  • 2
  • 17
  • 23