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.
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.
Comments