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…

subjects = []
lecturer = []

for line in x:
    f = line.split(',')
    if len(fields) == 8:
        subjects.append([0])
        lecturer.append(f[7])

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

while True:
    filter = input("Choose filter. Or [Q]uit: ")

    if filter.upper() == 'S':
        S(subjects)
        break
    elif filter.upper() == 'L':
        L(lecturer)
        break

Now if they choose L it runs this…

def L(lecturer):
    print ("""
Lecturers
---------
""")
    print (''.join(['[' + str(ind + 1) + '] ' + x  for ind, x in enumerate(lecturer)]))
    pick_lecturer = input("npick a lecturer: ")

Which outputs like:

[1] Joe Blo   
[2] Cat Blue   
[3] Kim Toll

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:

import csv
from collections import namedtuple

Item = namedtuple(
    "Item", "subject type day time_from time_to some_column1 some_column2 name"
)


def L(data):
    all_lecturers = list(set(d.name for d in data))

    for i, name in enumerate(all_lecturers, 1):
        print("[{}] {}".format(i, name))

    while True:
        try:
            pick_lecturer = int(input("npick a lecturer: "))
            lecturer_name = all_lecturers[pick_lecturer - 1]
            break
        except:
            continue

    for d in [d for d in data if d.name == lecturer_name]:
        print(d)


# 1. read the file
data = []
with open("your_file.txt", "r") as f_in:
    reader = csv.reader(f_in)
    for row in reader:
        data.append(Item(*row))

# 2. choose a filter
while True:
    filter_ = input("Choose filter. Or [Q]uit: ")

    if filter_.upper() == "Q":
        break
    # if filter_.upper() == "S":
    #     S(data)
    #     break
    elif filter_.upper() == "L":
        L(data)
        break

Prints:

Choose filter. Or [Q]uit: L
[1] Cat Blue
[2] Kim Toll
[3] Joe Blo

pick a lecturer: 3
Item(subject='ADF101', type='Lecture', day='Monday', time_from='08:00', time_to='10:00', some_column1='Jenolan', some_column2='J10112', name='Joe Blo')
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement