Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
code advice
#1
Hi,
I'm having a small issue with a code I wrote. What I'm trying to do is something small and simple. I want to open a .csv file, check if in that file there is a certain code and if it is, I want to print the whole line of the .csv file that contains that code.

This is what I have and it does what I need it to:
file_name = input('Enter file name: ')

open_file = open(file_name)

code = input('Enter code: ')

for line in open_file:
    comma = line.find(',')
    number = line[:comma]
    if code in number:
        print(line)
but here I would like to add a line that says 'no such code' if the code does not exist in the .csv file. So I did this:
    if code not in number:
        print('No such code!')
My problem is that this prints 'no such code! for each line that I have in my .csv file but I only want it to print once.
Can anyone give me some advice on how to fix my code?

Thanks so much!
Reply


Messages In This Thread
code advice - by just_me - Jun-05-2020, 07:20 PM
RE: code advice - by buran - Jun-05-2020, 07:26 PM
RE: code advice - by just_me - Jun-05-2020, 07:34 PM
RE: code advice - by buran - Jun-05-2020, 07:43 PM
RE: code advice - by just_me - Jun-05-2020, 08:09 PM
RE: code advice - by buran - Jun-05-2020, 08:13 PM
RE: code advice - by just_me - Jun-05-2020, 08:41 PM

Forum Jump:

User Panel Messages

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