Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Ignoring a list item
#1
Hi all!
I need a bit of help ignoring a list item, I have a list of names with a "Y" at then end of each name and I want to ignore names without a "Y", my code is below:

from time import strftime # import time
print("Report date: " + strftime("%d/%m/%Y")) #Prints clock time

with open("confPack.txt", "r") as confPack: # open file as confpack
    cPack = confPack.read().splitlines() # create cpak variable

with open("employees.txt") as fp:
    for line in fp:
        values = line.strip().split(",")
        surname = values[0]
        firstName = values[1]

        # is the last value not a Y (meaning no Y's)

        # if that's false, meaning there is a Y, is the one before it a Y too
        if values[-1] != 'Y':
            print(" did not attend ")
        elif values[-2] == 'Y':
            packs = print(cPack[1])
        else:
            packs = print(cPack[0])
        print(f"Attendee: {surname}, {firstName}: {packs}")
Thanks in advance!
Reply
#2
Lines 19 and 21 assign the value None to packs, so line 22 is either going to print "None" for {packs} or it is going to throw a name error because packs is not defined. Do you want to do something like this?
        if values[-1] != 'Y':
            packs = None
        elif values[-2] == 'Y':
            packs = cPack[1]
        else:
            packs = cPack[0]

        if packs is not None:
            print(f"Attendee: {surname}, {firstName}: {packs}")
Reply
#3
I don't understand what the objective is. Nevertheless there is built-in str.endswith which could be used:

>>> s = 'happy'
>>> s.endswith('y')
True
>>> s.endswith('p', 0, -1)
True
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  count item in list korenron 7 283 Jul-01-2021, 01:46 PM
Last Post: deanhystad
  Ignoring errors when using robjects. Rav013 3 467 May-04-2021, 09:05 PM
Last Post: Gribouillis
  Time.sleep: stop appending item to the list if time is early quest 0 356 Apr-13-2021, 11:44 AM
Last Post: quest
  How to run a pytest test for each item in a list arielma 0 485 Jan-06-2021, 10:40 PM
Last Post: arielma
  How do I add a number to every item in a list? john316 2 432 Oct-28-2020, 05:29 PM
Last Post: deanhystad
  Select correct item from list for subprocess command pythonnewbie138 6 904 Jul-24-2020, 09:09 PM
Last Post: pythonnewbie138
  Why is the item not in list when it is DanielCook 2 654 Jul-08-2020, 07:38 PM
Last Post: deanhystad
  best way to add item to list only once Phaze90 1 787 May-01-2020, 05:02 AM
Last Post: bowlofred
  item from a line to list however when i print the line instead of words i get letters Sutsro 5 897 Apr-22-2020, 02:39 PM
Last Post: deanhystad
  Help removing asterisk item in a nested list. bmcguire 3 793 Apr-06-2020, 02:35 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

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