[Tutor] Variable data to CSV

Cameron Simpson cs at zip.com.au
Sat Feb 28 23:54:26 CET 2015


On 27Feb2015 23:16, alan.gauld at btinternet.com <alan.gauld at btinternet.com> wrote:
>On 27/02/15 20:00, Thomas Toker wrote:
>>I need to find the device address, usb port, and number of times the error occurs
>
>OK, Assume we don;t know anything about USB
>Explain how that relates tom your sample data below.
>Which columns do you want to extract/process?
>Your data is not consistent in structure so using csv is
>probably more trouble than its worth.

The OP wanted to write the results of his scan as CSV. The input isn't CSV; it 
looks like Linux kernal demsg output with time-since-boot timestamp prefixes.

>Look at using split() based on both spaces and colons.

I'd be:
  - using split on space as suggested
  - discarding/skipping (possibly noting) the timestamp column
  - grabbing column 2 and checking for "hub" or "usb", ignoring other lines
  - grabbing column 3 as the device designator; looks like 1+2 is a useful device key
  - keeping some state for the counting, and resetting the counter when you see a new device
  - for extra points, later, keeping multiple state in case thiese processes ever overlap

I would suggest using "in" to check for strings and because they are very fixed 
in kernel messages, checking for long strings:

  # split line into words
  words = line.split()
  if "new high speed USB device using ehci_hcd and address" in line:
    # get port number from last field
    port = int(words[-1])

and so forth. Must simpler.

Regarding counters:

  # at start of script:
  counts = {}   # dict of counter based on port (or better, (device, port))

  # when new device seen (nb: possibly print out accumulated prior count value)
  count[port] = 0

  # when new error seen
  count[port] += 1

Chase CSV as a totally separat thing later; just use print() for now.

Cheers,
Cameron Simpson <cs at zip.com.au>

I am of course experienced in teaching and have read the writings of Socrates.
        - egnilges at phoenix.Princeton.Edu


More information about the Tutor mailing list