Skip to main content
deleted 20 characters in body; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

Python 3 program which allows inserting Inserting filename, reading data, inserting regex, and testing if each line matches

Program:This program first displays a menu which shows some options that the user can select. Each option will the respective functions which do some things.

The user can insert a filename, read data from it, enter a regex pattern, test if each line of the file matches the given regex pattern.

Please provide feedback on how I can improve my program.

The above program first displays a menu which shows some options that the user can select. Each option will the respective functions which do some things.

The user can insert a filename, read data from it, enter a regex pattern, test if each line of the file matches the given regex pattern.

Please provide feedback on how I can improve my program

Python 3 program which allows inserting filename, reading data, inserting regex, testing if each line matches

Program:

The above program first displays a menu which shows some options that the user can select. Each option will the respective functions which do some things.

The user can insert a filename, read data from it, enter a regex pattern, test if each line of the file matches the given regex pattern.

Please provide feedback on how I can improve my program

Inserting filename, reading data, inserting regex, and testing if each line matches

This program first displays a menu which shows some options that the user can select. Each option will the respective functions which do some things.

The user can insert a filename, read data from it, enter a regex pattern, test if each line of the file matches the given regex pattern.

Please provide feedback on how I can improve my program.

Source Link
Spikatrix
  • 1k
  • 1
  • 9
  • 21

Python 3 program which allows inserting filename, reading data, inserting regex, testing if each line matches

Program:

import re

filename = None
readData = None
regex    = None

def test_data():
    '''Checks if each line of data matches the regex'''
    
    global readData
    global regex

    if regex == None:
        print('Empty regex')
        print('')
        return None
    
    try:
        pattern = re.compile(regex)
    except:
        print('Invalid regex')
        print('')
        return None
    i = 1
    if readData == None:
        print('Read data is empty')
        print('')
        return None
    
    for line in readData:
        if pattern.match(line.strip()):
            print(' Line ', i, ':"', line,'"', ' matches current regex', sep='')
        else:
            print(' Line ', i, ':"', line,'"', ' does not match current regex', sep='')
        i += 1
    print('')

def change_regex():
    '''Sets the regex variable'''

    global regex
    print('Current regex is ', end='')
    if regex != None:
        print('"', regex, '"', sep='')
    else:
        print('empty')

    regex = input('Enter new regex: ')
    print('')
    
def read_file():
    '''Reads data from file if possible'''

    global filename
    global readData
    try:
        file = open(filename, "r")
        readData = file.readlines()
        print('Data read successfully!')
        file.close()
    except TypeError:
        print('filename is empty!')
    except FileNotFoundError:
        print('File not found!')

    print('')
        
def change_filename():
    '''Sets the filename variable'''

    global filename
    print('Current filename is ', end='')
    if filename != None:
        print('"', filename, '"', sep='')
    else:
        print('empty')
        
    filename = input('Enter new filename (with extension): ')
    print('')

def show_menu():
    '''Shows the menu'''

    print('Enter 1 to change filename')
    print('Enter 2 to read file')
    print('Enter 3 to change regex')
    print('Enter 4 to test if read data matches given regex')
    print('Enter 5 to exit')

def main():
    while True:
        show_menu()
        try:
            option = int(input('Option: '))
        except:
            print('Invalid input')
            print('Please enter a valid option')
            print('')
            continue
        if option == 1:
            change_filename()
        elif option == 2:
            read_file()
        elif option == 3:
            change_regex()
        elif option == 4:
            test_data()
        elif option == 5:
            break
        else:
            print('Invalid input')
            print('Please enter a valid option')
            print('')

if __name__ == "__main__":
    main()

The above program first displays a menu which shows some options that the user can select. Each option will the respective functions which do some things.

The user can insert a filename, read data from it, enter a regex pattern, test if each line of the file matches the given regex pattern.

Please provide feedback on how I can improve my program