简体   繁体   English

使用 Python 在 csv 文件中从字符串搜索中打印多列

[英]Print multiple columns from string search in csv file using Python

I have a csv file, the script below searches a string "PROGRAM" and prints out the data in the column which the string is located in. It only does this once though, there are multiple instances of "PROGRAM" found in the csv file, how can I print the additional columns where the string appears?我有一个 csv 文件,下面的脚本搜索字符串“PROGRAM”并打印出该字符串所在列中的数据。虽然它只执行一次,但在 csv 文件中找到了多个“PROGRAM”实例,如何打印字符串出现的附加列?

import os
import csv
from pathlib import Path
from collections import defaultdict

search_str = "PROGRAM"

searchfile = open("Test_Process.csv", "r")
for line in searchfile:

index = line.find(search_str)
if (index != -1):
columns = defaultdict(list)
with open('Test_Process.csv') as f:
reader = csv.reader(f)
for row in reader:
for (i,v) in enumerate(row):
columns[i].append(v)
b=(columns[index])
for x in b[:]:
print (x)

You really should post only propperly indentated code.你真的应该只发布适当缩进的代码。

What you, imho, shouldn't do:恕我直言,你不应该做的事情:

  • str.find doesn't give you a column number index. str.find不会为您提供号索引。 It does give you the index where the first occurence of search_str starts (or -1 if nothing is found) in the string that is searched (here line ).它确实为您提供了在搜索的字符串(此处为line )中第一次出现search_str的索引(如果没有找到,则为 -1)。 This doesn't lead to an error in your program because you're using a defaultdict .这不会导致程序出错,因为您使用的是defaultdict
  • Your code loads the csv file several times into a dict - that's not very efficient.您的代码将 csv 文件多次加载到字典中 - 这不是很有效。

I'd suggest you do something like:我建议您执行以下操作:

import csv
from collections import defaultdict

# Reading csv-file columnwise into dict & identifying columns with search string
search_str = "PROGRAM"
relevant_columns = set()
with open("Test_Process.csv", "r") as file:
    reader = csv.reader(file)
    columns = defaultdict(list)
    for row in reader:
        for i, item in enumerate(row):
            columns[i].append(item)
            if search_str in item:
                relevant_columns.add(i)

# Printing the columns with search string
for col in sorted(relevant_columns):
    print(f"Printing items from column {col}:")
    for item in columns[col]:
        print(item)

Comments:注释:

  • The first block reads the file into a dict, identifies the columns in which the search string occurs, and collects them in a set relevant_columns .第一个块将文件读入一个字典,识别出现搜索字符串的列,并将它们收集在一个 set relevant_columns中。
  • The second block is printing the results, ie the columns with the search string.第二个块是打印结果,即带有搜索字符串的列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM