如何在Python中搜索序列中的值?

如何在Python中搜索序列中的值?,python,sequence,Python,Sequence,我对编码很陌生,所以请容忍我。基本上,我正在编写一个程序,该程序将要求输入姓氏或id号,然后在.csv文件中搜索该单词并返回它所属的整个列。下面是我写的,如果用户选择按id搜索 method = input("Search by invoice id (id) or customer last name (lname)?: ") data = "data.csv" dataFile = open(data, "r") dataRows = dataFile.readlin

我对编码很陌生,所以请容忍我。基本上,我正在编写一个程序,该程序将要求输入姓氏或id号,然后在.csv文件中搜索该单词并返回它所属的整个列。下面是我写的,如果用户选择按id搜索

method = input("Search by invoice id (id) or customer last name (lname)?: ")
    data = "data.csv"
    dataFile = open(data, "r")
    dataRows = dataFile.readlines()

if method == "id":
    term = input("Enter the id: ")
    for line in dataRows:
        row = line.strip()
        newRow = row.split('\n')
        if term == newRow[0]:
            print(newRow)
        else:
            print("No matches found.")
这是csv文件:

以下是readlines()之后的打印方式:


由于invoice ID是第一列,所以我认为可以使用if term==newRow[0]找到它,但这不起作用。有什么建议吗?非常感谢。

如果您不想使用
csv
模块解决此问题,则应
,“
”上拆分字符串,而不是在
“\n”


这样,
newRow
的第一个元素将是发票id。

最简单的方法是使用
csv
模块:

method = input('Search by invoice id (id) or customer last name (lname)?: ')
term = input('Enter the term: ')

found_row = None     # To store the row if a match is found

with open('data.csv', newline='') as csvfile:
    reader = csv.DictReader(csvfile)

    for row in reader:
        # Compare term with Invoice ID if chosen method is ID
        # else compare with Last Name
        if term == (row['invoice id'] if method == 'id' else row['last name']):
            found_row = row

if found_row:
    print(row)
else:
    print('No match found')

如果您只允许用户按ID或姓氏进行搜索,则可以要求用户输入1表示ID,2表示姓氏,从而进一步简化输入。这种方法是不灵活的,但优点是用户不太可能在错误的情况下输入方法。例如,您正在比较
method==“id”
,但用户可能输入
id
id
。可能会使您的生活更简单。

在处理CSV时,您应该使用该模块。所有的行拆分和其他操作都会在you@RoadRunner我加的,谢谢!如果数据是逗号分隔的,它不应该是:
newRow=row.split(',')
?@aydow不幸的是我们的老师没有教我们这个,但这是个好主意@约翰尼莫:哦,这很有帮助!按发票id(id)或客户姓氏(lname)搜索?:id输入搜索词:111未找到匹配项。['111','Jim','Morrison','27','1','50.25']未找到匹配项。没有找到匹配项。没有找到匹配项。没有找到匹配项。没有找到匹配项。没有找到匹配项。['111',Robby',Krieger',21',1',64.45']未找到匹配项。['111',Freddie',Mercury',21',1',46.2']未找到匹配项。没有找到匹配项。没有找到匹配项。但是现在它一直说如果id不匹配,就找不到匹配项,这是有效的!有没有办法数一数出现了多少记录?例如,当搜索ID号111时,有三个单独的序列具有该ID号。我想添加“找到3条记录”,但我不太确定如何添加计数器并使用for循环遍历所有记录;您可以在
for
循环开始之前添加一条语句,如
counter=0
,并在
if
语句的正下方添加语句
counter+=1
。我做了:if-method==“id”:term=input(“输入搜索词:”)count=0,用于数据行中的行:row=line.strip()newRow=row.split(‘,’)if term==newRow[0]:count=+1 print(line)print(“{0}记录已找到)。format(count)),但不幸的是,这是结果111,Jim,Morrison,27,1,50.25 1记录已找到111,Robby,Krieger,21,1,64.45记录已找到111,Freddie,Mercury,21,1,46.2记录已找到
newRow = row.split(",")
method = input('Search by invoice id (id) or customer last name (lname)?: ')
term = input('Enter the term: ')

found_row = None     # To store the row if a match is found

with open('data.csv', newline='') as csvfile:
    reader = csv.DictReader(csvfile)

    for row in reader:
        # Compare term with Invoice ID if chosen method is ID
        # else compare with Last Name
        if term == (row['invoice id'] if method == 'id' else row['last name']):
            found_row = row

if found_row:
    print(row)
else:
    print('No match found')