[英]How do I print rows of a CSV file that have a specific keyword in them?
My current code only print out the exact input that matches the line in the CSV file.我当前的代码仅打印出与 CSV 文件中的行匹配的确切输入。
import csv
import sys
#input name you want to search
name = input('Enter name to find\n')
#read csv, and split on "," the line
csv_file = csv.reader(open('test.csv', "r"), delimiter=",")
#loop through the csv list
for row in csv_file:
#if current rows 2nd value is equal to input, print that row
if (name == row[0]):
print (row)
elif (name == row[1]):
print (row)
import csv
import sys
#input name you want to search
name = input('Enter name to find\n')
#read csv, and split on "," the line
csv_file = csv.reader(open('railway_stations.csv', "r"), delimiter=",")
#def error():
# print("error")
#loop through the csv list
for row in csv_file:
#if current rows 2nd value is equal to input, print that row
if (name in row[0].split()):
print (row[1])
if (name in row[0].split()):
print (row[0])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.