简体   繁体   中英

Can I use a Dictionary to store keywords that I need to loop through a csv file to find?

I am writing a python script that will go through a csv file row by row looking for a keyword. Once that keyword is found I need to write that entire row to a new .csv file. I am having trouble writing the for loop to complete this task and I do not understand how to write to a new .csv file. I will post what I have done so far below.

#!/usr/bin/python

# First import csv module to work on .csv file
import csv

#Lets open the files that will be used to read from and write too
infile = open('infile.csv','rb')
outfile = open('outfile.csv','w')


# Lets pass file object through csv reader method
csv_f_reader = csv.reader(infile)
writer = csv.writer(outfile,delimiter=',',quotechar='',quoting=csv.QUOTE_NONE)
#Lets create a dictionary to hold all search words as unique keys.
# the associated value will be used to keep count of how many successful
# hits the forloop hits.

search_word={'wifi':0,'WIFI':0,'wi-fi':0,'Wi-Fi':0,'Cisco':0,'cisco':0,'NETGEAR':0,'netgear':0,'Netge$

for csv_line in csv_f_reader:
    match_found = False
    for keyword in search_word.keys():
            for csv_element in csv_line:
                    if keyword in csv_element:
                            match_found = True
                            search_word[keyword] +=1
    if match_found:
            writer.writerow(csv_line)


#Dont forget to close the file
infile.close()
outfile.close()
print search_word.keys(), search_word.values()

You really don't need to use a dictionary for your keywords here. Err... wait, oh you want to keep track of how many times you see each keyword. Your description didn't say that.

Anyway, you should be looping through the lines in the file and the keys. The loop should probably look like this:

for line in csv_f_reader:
    for keyword in search_word.keys():
        if keyword in line:
            search_word[keyword] += 1
            writer.write(line)

infile.close()
outfile.close()

I haven't double checked that you're using the csv module correctly, but that should give you an idea of what it should look like.

You don't need a dictionary for what you're describing (unless you're trying to count up the keyword instances). search_word.keys() gives you a list anyway which is OK.

First you want to iterate through the csv like this:

infile = open('infile.csv')
csv_f_reader = csv.reader(infile)
for csv_line in csv_f_reader:
    print csv_line

If you try that, you'll see that each line gives you a list of all the elements. You can use your list of keywords to compare each one and write the ones that pass

for csv_line in csv_f_reader:
    for k in search_word.keys():
        if k in csv_line:
             writer.writerow(csv_line)

In your case, the keywords aren't exactly the same as the CSV elements, they're inside them. We can deal with this by checking the elements for substrings:

for csv_line in csv_f_reader:
    match_found = False
    for k in search_word.keys():
        for csv_element in csv_line:
            if k in csv_element:
                match_found = True
    if match_found:
        writer.writerow(csv_line)

One other thing, is you need to open the output file in write mode with:

outfile = open('outfile.csv', 'w')

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