Jun-05-2020, 07:20 PM
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:
Can anyone give me some advice on how to fix my code?
Thanks so much!
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!