Skip to content
Advertisement

Python – Trying to print each line that has a string value that matches the index provided

Now I’m still pretty new to python and programming in general, but I know I’ve gone a bit of a roundabout way this entire program. But here is what I have and what I want if anyone can help.

Begins by reading a text file e.g.

ADF101,Lecture,Monday,08:00,10:00,Jenolan,J10112,Joe Blo
ADF101,Tutorial,Thursday,10:00,11:00,Jenolan,J10115,Cat Blue
ALM204,Lecture,Monday,09:00,11:00,Tarana,T05201,Kim Toll

Then I make empty lists and append them with each index…

JavaScript

Then I provide an input that runs a function based on the input.

JavaScript

Now if they choose L it runs this…

JavaScript

Which outputs like:

JavaScript

Here’s where I’m stuck. I want to make it so that if the last
input is ‘1’ it will read the file for each line with Joe Blo
and print the entire line. Without any external modules or libraries
Any guidance is appreciated. Thanks.

Advertisement

Answer

You can use csv module to read the file into a list. In this example, I read each row from the file into a namedtuple:

JavaScript

Prints:

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement