0

I have a program that takes file code as input in this case '19E071OE1' and it needs to print out all the rows that have that value in its 4th column it kind of works but I have to input file code for each individual row. My question is how do I make it scan all rows at once and print them out?

Code:

import csv


def ime_predmeta():
    imepred = input("Unesi kod predmeta: ")
    if imepred.isupper():
        return imepred
    else:
        print("Unos nije validan probajete opet!")
        return ime_predmeta()


def ucitavanjerasp1():
    file = open('raspored1.csv', "r")
    reader = csv.reader(file, delimiter=',')
    for row in reader:
        if ime_predmeta() in row[3]:
            print(row)


def main():
    ucitavanjerasp1()


if __name__ == '__main__':
    main()

The bad output:

Unesi kod predmeta: 19E071OE1
['0', '08:00', '11:00', '19E071OE1 [OO 2019] P1']
Unesi kod predmeta: 19E071OE1
['0', '11:00', '14:00', '19E071OE1 [OO 2019] P2']
Unesi kod predmeta: 19E071OE1
['0', '14:00', '17:00', '19E071OE1 [OO 2019] P3']
Unesi kod predmeta: 19E071OE1

1 Answer 1

1

You can achieve that by using regex split method like

with open('raspored1.csv', "r") as f:
    content = f.read()
    print(re.split(f'.*\,.*\,.*\,.*{imepred}.*',content))

this will return you a list with all lines that contain imepred value init in the 4 column.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.