rmackenzie
rmackenzie

Reputation: 31

Raspberry PI Writing CSV Python

As part of a project, I am trying to use a Raspberry PI to capture WiFi networks and write it to a CSV file. If the quality of the signal is over 30/70 I want to capture all WiFi SSIDs and their relevant MAC address and record it. The issue seems to be relevant syntax but I cannot seem to figure out what is wrong.

def wifiscan():
    ssid = []
    scanoutput = check_output(["iwlist", "wlan0", "scan"])
    curtime = time.strftime("%I:%M:%S")
    ssid.append(curtime)

    for line in scanoutput.split():
      line=str(line)
      if line.startswith("Quality"):
        line=line[8:-25]
      if(line>30 and line.startswith("ESSID")
        line=line[7:-1]
        ssid.append(line)
    with open('/home/pi/Desktop/Project/Results/'+'test.csv','a') as csvfile:
        csvwriter = csv.writer(csvfile,delimiter=',')
        csvwriter.writerow(ssid)
    print ssid

Update 1: This is the output that I am looking to capture from: Update 2: This is what happens with the updated code and the results out.

Upvotes: 2

Views: 1696

Answers (1)

DSLima90
DSLima90

Reputation: 2848

I made some changes to your code but i don't know exactaly what you want to accomplish nor what errors you are experiencing, I just fixed some logic an sintax.

As it is, the code will append the ssid data to your csv only if quality is better than 30.

Let me know if that is what you want.

def wifiscan():
    ssid = []
    scanoutput = check_output(["iwlist", "wlan0", "scan"])
    curtime = time.strftime("%I:%M:%S")
    ssid.append(curtime)
    quality = 0
    essid = ""
    for line in scanoutput.split('\n'):
      line=str(line)
      if line.startswith("Quality"):
        quality=int(line[8:-25])

      if quality>30 and line.startswith("ESSID"):
        line=line[7:-1]
        ssid.append(quality)
        ssid.append(line)
        with open('/home/pi/Desktop/Project/Results/'+'test.csv','a') as csvfile:
            csvwriter = csv.writer(csvfile,delimiter=',')
            csvwriter.writerow(ssid)
    print ssid

Note that the code is sensible to any change in the sintax of the input you provide.

Also, to avoid syntax change issues in your input, maybe you should read about regular expressions in python: https://docs.python.org/2/howto/regex.html

Upvotes: 2

Related Questions