Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从.csv文件中找到值时如何停止For循环_Python_Python 3.x - Fatal编程技术网

Python 从.csv文件中找到值时如何停止For循环

Python 从.csv文件中找到值时如何停止For循环,python,python-3.x,Python,Python 3.x,我是一名网络工程师,试图学习使用Python编程。我对任何形式的编程都很陌生 我试图从.csv文件中搜索第1列中的值,并返回相应列的字符串。当程序执行时,它在所有单元格中循环,显示每个单元格的结果。我想要的是,如果用户输入一个数字,那么它需要搜索列表,一旦找到匹配项,就返回下一列的字符串。如果用户输入的值不存在,则返回打印语句 import csv import sys fileName = 'sip.csv' READ = 'r' WRITE = 'w' enter_code = inpu

我是一名网络工程师,试图学习使用Python编程。我对任何形式的编程都很陌生

我试图从.csv文件中搜索第1列中的值,并返回相应列的字符串。当程序执行时,它在所有单元格中循环,显示每个单元格的结果。我想要的是,如果用户输入一个数字,那么它需要搜索列表,一旦找到匹配项,就返回下一列的字符串。如果用户输入的值不存在,则返回打印语句

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")
结果:

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.
你的意思是这样的:

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")

像这样的东西应该适合你

# Select All From CSV File Where

import csv
# Asks for search criteria from user
search_parts = input("Enter search criteria:\n").split(",")
# Opens csv data file
file = csv.reader(open("C:\\your_path\\test.csv"))
# Go over each row and print it if it contains user input.
for row in file:
    if all([x in row for x in search_parts]):
        print(row)

您的问题还不完全清楚,一个
sip.csv
示例会有所帮助。对于一些示例
sip.csv
文件,与您得到的输出相比,您想要的输出是什么?嗨,Dillon。运行代码时会得到什么错误或不正确的结果?@Hoog,谢谢你回来找我。基本上,我有一个表格,包含+/-2000行.csv格式的数据。我希望用户在第一列的任意位置搜索一个值,并从下一行返回信息。如果找到匹配项,则代码需要停止循环。如果找不到匹配项,则打印一条消息,@bart cubrich,感谢您的回复。你的建议奏效了。我不必添加分隔符=“;”语句。它完全符合我的要求。非常感谢。现在到任务的下一个阶段,试着使用TKiTter并为这个做一个GUI。狄龙请考虑标记“the thyc0d'的答案为接受(左边的检查标记),因为它对你起作用。谢谢你让我知道”GLHR。对这个世界来说非常陌生:)