简体   繁体   English

如何打印 CSV 文件中包含特定关键字的行?

[英]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.

相关问题 如何打印其中包含特定关键字的csv文件行 - How do I print rows of a csv file that have a specific keyword in them 如何在csv文件中的行中打印特定字段,以及如何将输入内容写入csv文件? - How do I print specific fields from rows in a csv file and how to write input to a csv file? 我在 .csv 文件的特定列中有 MongoDB 格式的字符串行 如何将其转换为数据帧? - I have rows of strings in the MongoDB format in a specific column in the .csv file How do I convert it into dataframe? 如何查询我已输入的csv文件的特定列并使用python打印所有返回的行? - How can i query a specific column of a csv file that i have input and print all returned rows using python? 如果我有一个CSV文件的Python列表,如何将它们全部合并为一个巨型CSV文件? - If I have a Python list of CSV files, how do I merge them all into one giant CSV file? 如何通过搜索特定关键字在列表中打印列表? - How do I print the list in a list by searching for a specific keyword? 如何在特定列中具有特定值的 CSV 文件中打印特定行? - How to print specific rows in a CSV files which have a specific value in a specific column? 如何读取文件中的特定列并将它们变成新的 csv - How do I read specific columns in a file and make them into a new csv 如何从使用熊猫上传的大型CSV文件中仅打印出某些行 - How do I print out only certain rows from a large CSV file uploaded using pandas 如何搜索关键字并打印文件,只打印关键字中的行 - How do I search a keyword and print a file with only print the lines from the keyword on
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM