Search code examples
pythonfilereadfile

Reading specific area of string from file


I'm trying to figure out how to do some stuff with python. I got a text file, which containes strings i.e. :

M, 1, 14/08/2019 11:39, 4, xxxx, name, “Initialization of the system, and loading
M, 1, 14/08/2019 11:40, 100, xxxx, name, “Open Connection”
M, 1, 14/08/2019 11:40, 100, xxxx, name, “Close Connection, and reboot”
S, 1, 14/08/2019 11:40, 6, xxxx, name, We created the user in the systems
S, 1, 14/08/2019 11:41, 3, xxxx, User logged in, User tal logged in
M, 1, 14/08/2019 11:39, 4, xxxx, name, “Initialization of the system”
S, 1, 14/08/2019 11:40, 6, New User, We created the user in the systems
S, 1, 14/08/2019 11:41, 3, User logged in, User tal logged in
S, 1, 14/08/2019 11:42, 3, User logged in, User tal logged in
M, 2, 14/08/2019 11:43, 100, yyy, yura, 12345, Message

What I'm trying to do is getting into the file, and if its the first time there is M,1 I should print some text, same if its S,1 or M,2 or S,1. I also have to print only selected rows from the file (didnt make it yet but I will with a row counter). What I also have to do is to print only selected columns, what I mean by columns is there is a seperator ',' between columns, i.e if I want to print 3 and 4 colums of rows 1 and 2 I should print only 14/08/2019 11:39 , 4 and 14/08/2019 11:40 , 100. I figured out already how to split the strings with re.split, But I have no idea how to continue. Thanks.

import re
import string
filename = '11.txt'
def infile(filename):
    m1 = m2 = s1 = s2 = 0
    linecounter = 1
    lines = [1,2,3]
    colums = [2,4]
    i=0
    fin = open(filename, 'r')
    if fin.closed:
        print ('file is closed')
    lines = fin.readlines()
    for line in lines:
        if(line[0] == 'M' and line[3] == '1' and m1 == 0):
            print('---M, 1, Datetime, Error Level, DeviceId, UserId, Message---\n')
            m1 = 1
        elif (line[0] == 'M' and line[3] == '2' and m2 == 0):
            print('---M, 2, Datetime, Error Level, DeviceId, UserId, MobileId, Message---\n')
            m2 = 1
        elif (line[0] == 'S' and line[3] == '1' and s1 == 0):
            print('---S, 1, Datetime, Error Level, DeviceId, Action, Message---\n')
            s1 = 1
        elif (line[0] == 'S' and line[3] == '2' and s2 == 0):
            print('---S, 2, Datetime, Error Level, DeviceId, IP, Action, Message---\n')
            s2 = 1
        for p in re.split(",",line): // thats a check of spliting, nothing else
            print("piece="+p)
        print(line)

infile(filename)

Solution

  • I have created a function below select_columns that will take an array of int's (for the columns) and then split the line by the , delimeter and return a string of the collated values.

    Hope this helps

    import re
    import string
    filename = '11.txt'
    column_list = [3, 4] #Index 1 not index 0
    def infile(filename, column_list):
        m1 = m2 = s1 = s2 = 0
        linecounter = 1
        lines = [1,2,3]
        colums = [2,4]
        i=0
        fin = open(filename, 'r')
        if fin.closed:
            print ('file is closed')
        lines = fin.readlines()
        for line in lines:
            if(line[0] == 'M' and line[3] == '1' and m1 == 0):
                print('---M, 1, Datetime, Error Level, DeviceId, UserId, Message---\n')
                print(select_columns(row = line, column_list = column_list))
                m1 = 1
            elif (line[0] == 'M' and line[3] == '2' and m2 == 0):
                print('---M, 2, Datetime, Error Level, DeviceId, UserId, MobileId, Message---\n')
                print(select_columns(row = line, column_list = column_list))
                m2 = 1
            elif (line[0] == 'S' and line[3] == '1' and s1 == 0):
                print('---S, 1, Datetime, Error Level, DeviceId, Action, Message---\n')
                print(select_columns(row = line, column_list = column_list))
                s1 = 1
            elif (line[0] == 'S' and line[3] == '2' and s2 == 0):
                print('---S, 2, Datetime, Error Level, DeviceId, IP, Action, Message---\n')
                print(select_columns(row = line, column_list = column_list))
                s2 = 1
            for p in re.split(",",line): # thats a check of spliting, nothing else
                print("piece="+p)
            print(line)
    
    def select_columns(row, column_list):
        column_split = row.split(',')
        return_string = ''
        for column in column_list:
            return_string = '{0},{1}'.format(return_string, column_split[column - 1])
        return return_string[1:] # retruns the string trimming the first comma
    
    
    infile(filename, column_list)