How to Stop a For Loop when a value is found from a .csv file

Dillon

I'm a Network Engineer trying to learn programming using Python. I'm very new to any form of programming.

I'm trying to search for a value in column 1 from a .csv file and return the string of corresponding column. When the program is executed, it loops through all the cells, displaying the result of each cell. What I want is if a user enters a number, then it needs to search the list, once a match is found, return the next column's string. If a user entered a value that doesn't exist then return a print statement.

import csv
import sys

fileName = 'sip.csv'
READ = 'r'
WRITE = 'w'

enter_code = input('Enter The Sip Response Code: ')
with open(fileName, READ) as myXLfile:
    dataFromFile = csv.reader(myXLfile)
    for currentRow in dataFromFile:
        if enter_code == currentRow[0]:
            print("The SIP Response Code you entered is: " + enter_code)
            print("The SIP Message is:  " + currentRow[1])
            print("Meaning:  " + currentRow[2])
            break
        if enter_code != currentRow[0]:
            print("Im Sorry, I Do not have this Response Code")
    else:
        print("Thank You and Goodbye")

Result:

Enter The Sip Response Code: 200
Im Sorry, I Do not have this Response Code
Im Sorry, I Do not have this Response Code
Im Sorry, I Do not have this Response Code
Im Sorry, I Do not have this Response Code
Im Sorry, I Do not have this Response Code
Im Sorry, I Do not have this Response Code
The SIP Response Code you entered is: 200
The SIP Message is:  OK
Meaning:  The request has been successfully processed and the result of the request is transmitted in the response.
The_C0der

you mean like this:

import csv

fileName = 'sip.csv'
READ = 'r'
WRITE = 'w'
check = True

enter_code = input('Enter The Sip Response Code: ')
with open(fileName, READ) as myXLfile:
    dataFromFile = csv.reader(myXLfile, delimiter=";")
    for currentRow in dataFromFile:
        if enter_code == currentRow[0]:
            print("The SIP Response Code you entered is: " + enter_code)
            print("The SIP Message is:  " + currentRow[1])
            print("Meaning:  " + currentRow[2])
            check = False
            break
    if check:
        print("Im Sorry, I Do not have this Response Code")
    else:
        print("Thank You and Goodbye")

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I make a search statement in java with a while loop and stop and return the object when it's found

How to stop the loop and add the value?

C#: How to stop looping through data in a csv file and store last read value

How to change value in csv column by value from another file

How to stop a stack buffer overflow when reading from file?

How to stop reading pipe() when '\0' is found

How to stop running loop when identical array is found?

How to stop Pandas from removing enclosing characters from a csv file?

How to remove stop words from a csv file

How can I stop gvim from crashing when opening a file?

Stop eclipse from resetting root context when Maven->Update Project / MANIFEST.MF file not found

How to stop storing characters from a file when there is a space

How to stop a for loop from running?

How to get NULL from LINQ When no value found in the joining tables

Stop a loop if information is not found

How to loop through multiple URLs to scrape from a CSV file in Scrapy?

Value not updating when taken from PHP file in a loop

How to get next n lines from in a file when a line is found

How to stop variable from going back to default value when read in different file

How to prompt the program to continue reading other csv files in csv folder even when one of the csv file is not found?

How to stop the loop if the value is not found?

If statement in a for loop to stop iteration if a substring is found in a hash value returned by a function

How to stop recursion when an answer is found in Python?

How to save multiple values in CSV file from loop in python?

Stop a loop when a value is found and then add values to a list

File not found when appending a csv file

How to stop a loop when the end of a word document is found?

How to save a dictionary value to a csv file in python within a loop?

How to loop through a JS set and break when a value is found?