0

So I am trying to break this CSV data down a bit more, and grab a specific comma delimited piece of text. It is a wireshark CSV.

import csv

with open('wifi.csv', 'rt') as csvfile:
    reader = csv.reader(csvfile, delimiter='\n', quotechar='\n')
    for row in reader:
        print(" ".join(row[0:1]))

Which only prints out rows. These rows contain comma delimited objects that are enclosed in double quotes. I'm trying to find a way to access these effectively.

Output: "85","23.128318","52.86.227.189","10.0.0.23","TCP","56","[TCP Retransmission] 443 > 61541 [FIN, ACK] Seq=32 Ack=33 Win=118 Len=0" "86","26.766224","fe80::1286:8cff:fe4f:d04d","ff02::1","ICMPv6","174","Router Advertisement from 10:86:8c:4f:d0:4d" "87","27.479193","10.0.0.23","104.70.61.253","SSL","55","Continuation Data"

I want to access these individually, so I can grab a specific one. But it isn't like a list or anything, and it is thinking it is string indicies.

1 Answers1

1

The csv module has the ability to read this format data and return the row as a list of strings. I think your problem is that you are taking the list and concatenating it together.

with open('wifi.csv') as csvfile:
    reader = csv.reader(csvfile)
    for row in reader:
        print("The source is %s" % row[3])
        print("The protocol is %s" % row[4])

should work. You do not need to pass delimiter or quotechar parameters. In this case they are causing your problem because you are saying each element is delimited by a newline (instead of the default comma). So you end up with the entire line being considered a single element.

You have your open as passing "rt" which is not a valid mode. "r" (read) is the default. If you want to both read and write to the file (which is not the case here) you can pass "r+" (a plus sign is the second character).

verisimilidude
  • 475
  • 6
  • 9