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
#2
is it possible that code is present more than once in the file (i.e. if found can you break out of the loop)?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
The code will only be present not more than once in the file
Reply
#4
file_name = input('Enter file name: ')
code = input('Enter code: ')
with open(file_name) as open_file:
    for line in open_file:
        number, *rest = line.split(',') # split at comma
        if code in number:
            print(line, end='') # line already has new line at the end. end='' will ensure no extra blank row
            break
    else:
        print(f'Code {code} not found in the file')
else part of the loop will be executed only if it not break out of it.
Note that code in number will be True for partial match, e.g '234' in '12345' is True
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
Thanks a lot buran. It is interesting to see how you changed the code. I also tried to modify my original code by adding the break and unindenting the else statement like you did in yours and it seems to work too. So I will keep both versions of the code for comparison.

You brought up another interesting fact; if I wanted to only match the exact code and not the partial code, how would I go about that?
Reply
#6
(Jun-05-2020, 08:09 PM)just_me Wrote: if I wanted to only match the exact code and not the partial code, how would I go about that?
use '==', i.e. equality. Still note that if there is only one column (i.e. no comma), number will have new line '\n' at the end. You will need to strip() it. but I think that's not the case here
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
Thanks again, I really appreciate your help and suggestions.
You're right that is not the case here, but it is still good to know.
Reply


Forum Jump:

User Panel Messages

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