[Tutor] Variable data to CSV

Alan Gauld alan.gauld at btinternet.com
Sat Feb 28 00:16:12 CET 2015


On 27/02/15 20:00, Thomas Toker wrote:
> Hey all so I'm new to python(like 2 days ago new)

Welcome.
However it will help if:
1) You always post in plain text. As you see below HTML code gets zapped 
in email.


> 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.
Look at using split() based on both spaces and colons.


> import re import csv
> import reimport csvf = open("file.txt", "r") #location of log file
> searchlines = f.readlines()                   #create list from filef.close()for element in searchlines:     usbPres = re.search('(USB)',element)  #pattern to find usb lines     devAddr = re.search('(address)\s\d+',element)  #parsing pattern for device address     port = re.search('(port)\s\d',element)  #parsing pattern for port     if usbPres:         This is where I get lost because I want to assign the correct port to device address and then count number of time it failed before a new device is inserted into that port. then write it to a CSV

But the code is too messed up to read easily so please repost
in plain text.


>
> **** SAMPLE TEXT ***
> [11883.112089] hub 1-0:1.0: Cannot enable port 3. Maybe the USB cable is bad?
> [11883.224080] usb 1-7: new high speed USB device using ehci_hcd and address 42
> [11883.328151] hub 1-0:1.0: unable to enumerate USB device on port 7
> [11904.472097] hub 1-0:1.0: Cannot enable port 3. Maybe the USB cable is bad?
> [11907.440096] hub 1-0:1.0: Cannot enable port 3. Maybe the USB cable is bad?
> [11910.408093] hub 1-0:1.0: Cannot enable port 3. Maybe the USB cable is bad?
> [11913.376095] hub 1-0:1.0: Cannot enable port 3. Maybe the USB cable is bad?

HTH
-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list