sjo91190
sjo91190

Reputation: 69

for loop through a set list of strings in Python to create a filter

I am trying to find a more efficient way to filter through a list of SSIDs in a .csv file and then write the filtered results to another file.

Currently, the code I have looks like this (And it works as expected):

def ssidFilter():

    File = open("/home/pi/unFilter.csv", "r")
    for line in File:
        if 'Test SSID' in line:
            with open("/home/pi/dataLog.csv", "a") as finalFile:
                            finalFile.write(str(line))
        if 'Public' in line:
            with open("/home/pi/dataLog.csv", "a") as finalFile:
                            finalFile.write(str(line))
        if 'HomeNet' in line:
            with open("/home/pi/dataLog.csv", "a") as finalFile:
                            finalFile.write(str(line))
        if 'Network 1' in line:
            with open("/home/pi/gpsMaster/dataLog.csv", "a") as finalFile:
                            finalFile.write(str(line))
        if 'LimeOak' in line:
            with open("/home/pi/dataLog.csv", "a") as finalFile:
                            finalFile.write(str(line))
        if 'BlackCrow' in line:
            with open("/home/pi/dataLog.csv", "a") as finalFile:
                            finalFile.write(str(line))
        if 'GuestWiFi' in line:
            with open("/home/pi/dataLog.csv", "a") as finalFile:
                            finalFile.write(str(line))                                
    File.close()

However, I'd like to use a solution that isn't running through each of the SSIDs separately. Id like to use a list and iterate through it. I asked a question about the zip function this morning, but wasn't able to get it working with this part of the code.

What I've been working with (Has NOT worked):

SSID = ['Test SSID' , 'Public' , '....etc...']
File = open("/home/pi/unFilter/csv" , "r")
for line in File:
    if SSID in line:
        with open("/home/pi/dataLog.csv", "a") as finalFile:
            finalFile.write(str(line))
File.close()                               

The output of this yields an error at 'if SSID in line:' as "TypeError: 'in ' requires string as left operand, not list."

When substituting 'if SSID' with 'if str(SSID)', I get no error, but also nothing returns and I'm guessing that's because its searching for that whole string instead of the individual elements.

I've had the same TypeError, except its treating it as a tuple and not a list, when trying to use zip, if I'm using it correctly..

File = open('/home/pi/unFilter.csv', 'r')
for line in File:
    for new in zip(SSID):
        if new in line:
            print line

How should I approach this issue?

Upvotes: 1

Views: 439

Answers (2)

Jan Vlcinsky
Jan Vlcinsky

Reputation: 44112

One solution is to use any (as described in another answer), but in case, you are able to parse the name of SSID from your line, you can use more efficient solution just testing membership of found SSID name in list of names of interest.

Following code uses get_ssid function, which attempts to get the SSID name from line.

Having SSID name, the test is much simpler and faster.

def ssidFilter(ssids_to_log, input_csv, output_csv):
    def get_ssid(line):
        # parse SSID from the line, return it
        # Here I assume it is first part of the line, delimited by ;
        return line.split(";", 1)[0]

    with open(input_csv) as in_f:
        with open(output_csv, "a") as out_f:
            for line in in_f:
                if get_ssid(line) in ssids_to_log:
                    out_f.write(str(line))

if __name__ == "__main__":
    ssids_to_log = ["Test SSID", "Public", "HomeNet", "Network 1",
                    "LimeOak", "BlackCrow", "GuestWiFi"]
    input_csv = "/home/pi/unFilter.csv"
    output_csv = "/home/pi/dataLog.csv"
    ssidFilter(ssids_to_log, input_csv, output_csv)

Upvotes: 1

Cory Kramer
Cory Kramer

Reputation: 117886

You could use any to iterate through the keywords

SSID = ['Test SSID' , 'Public' , '....etc...']
with open('/home/pi/unFilter.csv', 'r') as in_file:
    for line in infile:
        if any(item in line for item in SSID):
            with open("/home/pi/dataLog.csv", "a") as finalFile:
                finalFile.write(str(line))

Upvotes: 2

Related Questions