Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Print multiple record lines
#1
I am creating a simple program that looks up the name of a job and prints out the full job name and number.

I want to be able to put in partial string and have the program print any job name that has that string.

ie. user enters "short"
program prints "Short North Phase 3 - 2016-0401"
"Short North Phase 2 - 2016-0405"
"Short North Public Outreach - 2016-0551"

I am stuck on the printing multiple lines that match the input string.
here is my current code:

while True:
    jobname=input("Please enter job name")
    jobname=jobname.upper()



    jobs = open("jobs.txt")
    for line in jobs:
        record = line.split('|')
        if jobname in record[0]:
            found = True
            break
        else:
            found = False
            continue

    if found == False:
        print("Job name not found")
    else:
        print(record[0], record[1])
any help would be greatly appreciated.
Reply
#2
Instead of breaking when you find a match, maybe keep a list of matches?
matches = []
with open("jobs.txt") as jobs:
    for line in jobs:
        record = line.split("|")
        if jobname in record[0]:
            matches.append(record)
if matches:
    for record in matches:
        print(record[0], record[1])
else:
    print("Job name not found")
Reply
#3
That is genius. Works perfectly.
Reply
#4
You can also make a generator, which is cool :p
If your jobs file is massive, and you could potentially have millions of matches, this is something similar to what you'd want to be using.

def find_within(haystack, match, processor=None):
    with open(haystack) as file:
        for line in file:
            if processor:
                line = processor(line)
            if match(line):
                yield line

matches = find_within("jobs.txt",
    lambda job: job[0] == jobname,
    lambda line: line.split("|"))

for match in matches:
    print(match[0], match[1])
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Last record in file doesn't write to newline gonksoup 3 404 Jan-22-2024, 12:56 PM
Last Post: deanhystad
  How do I stream and record at the same time with arducam? traderjoe 0 456 Oct-23-2023, 12:01 AM
Last Post: traderjoe
  How to write the condition for deleting multiple lines? Lky 3 1,129 Jul-10-2022, 02:28 PM
Last Post: Lky
  Delete multiple lines from txt file Lky 6 2,278 Jul-10-2022, 12:09 PM
Last Post: jefsummers
  failing to print not matched lines from second file tester_V 14 6,064 Apr-05-2022, 11:56 AM
Last Post: codinglearner
  Display table field on multiple lines, 'wordwrap' 3python 0 1,765 Aug-06-2021, 08:17 PM
Last Post: 3python
  pulling multiple lines from a txt IceJJFish69 3 2,570 Apr-26-2021, 05:56 PM
Last Post: snippsat
  why print('\n') produced 2 new lines instead of 1 - Located inside a FOR loop JulyFire 2 2,515 Jan-10-2021, 01:50 AM
Last Post: JulyFire
Question How to print multiple elements from multiple lists in a FOR loop? Gilush 6 2,924 Dec-02-2020, 07:50 AM
Last Post: Gilush
  How to print string multiple times on new line ace19887 7 5,715 Sep-30-2020, 02:53 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020