简体   繁体   中英

How do I print rows of a csv file that have a specific keyword in them

I'm trying to open two csv files, one with data (minidata.csv) and one with keywords (minikeys.csv), and search through the first one for keywords from the second one, and then print out the lines from the first one that include the keywords from the second one. Hope that makes sense.

I've tried opening the keywords file (minikeys.csv) as a list and searching from there, but I've come the closest to success by opening it into a dictionary for some reason.

with open('minidata.csv', 'r') as f:    
    text = f.read()
    csvFileArray = []

    with open('minikeys.csv', 'r') as inf:
        reader = csv.reader(inf)
        mydict = {rows[0] for rows in reader}

    for key in mydict:
        for row in text:
            if key in text:            
                print(row)

This will get it to print out every line in the minidata.csv file, not the matching ones, but it also prints out each character as many times as there is a character in the minikeys. So it'll give me output like:

aaaa,,,,bbbb,,,,cccc,,,,dddd...

instead of printing out the lines that match.

What should I do instead to get this to work?

Instead of

text = f.read()

do

text = f.readlines()

The issue here is that you're reading it as one big long string with the newlines included - whereas you want to be reading as a list of lines. In essence, f.readlines() is roughly equivalent to f.read().split('\\n') (not entirely, but similar enough for this particular comparison). Hence, why you see the output you do - you're iterating per character , not per line .

Changing text so that it ends up as a list of strings rather than just one string should fix your issue.


Also, minor terminology thing. You said mydict = {rows[0] for rows in reader} is a dict . It's not - it's a set . dict s are specifically for key-value pairs, whereas set s are just keys. They're both implemented as hashtables.

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