简体   繁体   中英

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?

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. 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 ). This doesn't lead to an error in your program because you're using a defaultdict .
  • Your code loads the csv file several times into a dict - that's not very efficient.

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 .
  • The second block is printing the results, ie the columns with the search string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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