Re: [Tutor] Variable data to CSV
On 27Feb2015 23:16, alan.ga...@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 I am of course experienced in teaching and have read the writings of Socrates. - egnil...@phoenix.princeton.edu ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Variable data to CSV
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 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Variable data to CSV
Hey all so I'm new to python(like 2 days ago new) I need to find the device address, usb port, and number of times the error occurs Heres what I have: 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 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? [11913.616090] usb 1-7: new high speed USB device using ehci_hcd and address 47 [11913.716121] hub 1-0:1.0: unable to enumerate USB device on port 7 [11927.340096] hub 1-0:1.0: Cannot enable port 3. Maybe the USB cable is bad? [11930.308096] hub 1-0:1.0: Cannot enable port 3. Maybe the USB cable is bad? [11933.276124] hub 1-0:1.0: Cannot enable port 3. Maybe the USB cable is bad? [11936.244118] hub 1-0:1.0: Cannot enable port 3. Maybe the USB cable is bad? [11939.212116] hub 1-0:1.0: Cannot enable port 7. Maybe the USB cable is bad?Any help is much appreciated.Tom ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor