-1

now I'm listing out all the usb's info, and just want to extract certain one , "Mass Storage Device"

my expect output is:

idVendor  idProduct  Manufacturer - Product
    1133      49271  Logitech - USB Optical Mouse
    1423      37728   - USB Reader
   34148       4096  JetFlash - Mass Storage Device
    7531          2  Linux 5.15.0-48-generic xhci-hcd - xHCI Host Controller
    7531          3  Linux 5.15.0-48-generic xhci-hcd - xHCI Host Controller
    5117       2112  Generic - External
    7531          2  Linux 5.15.0-48-generic xhci-hcd - xHCI Host Controller
    7531          3  Linux 5.15.0-48-generic xhci-hcd - xHCI Host Controller

(Mass Storage Device is  :  34148       4096  JetFlash  - Mass Storage Device)

I research that I can extract data is using regular expression, and re.findall, or by using if from here
Python regex to match a specific word if <keyword> in str: print('that line')

I'm stocking on slicing the print string, and correctly print expected line out, the tried code is below:(trying to use if condition)

import usb.core
import usb.backend.libusb1
import re

def list_USB_all_info():
    print ( 'idVendor  idProduct  Manufacturer - Product')
    

    busses = usb.busses()
    for bus in busses:
        for dev in bus.devices:
            if dev:
                xdev = usb.core.find(idVendor=dev.idVendor, idProduct=dev.idProduct)
                if xdev._manufacturer is None:
                    xdev._manufacturer = usb.util.get_string(xdev, xdev.iManufacturer)
                if xdev._product is None:
                    xdev._product = usb.util.get_string(xdev, xdev.iProduct)
                print ('%8d  %9d  %s - %s' % (dev.idVendor, dev.idProduct,
                                            str(xdev._manufacturer).strip(),
                                            str(xdev._product).strip()))
    
    if "Mass Storage Device" in str:
        print('that Mass Storage Device line info')
    
    list_certain_data = "Mass Storage Device is  : "
    list_Mass_Storage_Device = (str(dev.idVendor) + "  "+ str(dev.idProduct)+ "  |  "+str(xdev._manufacturer)+ "  |  "+str(xdev._product))
    
    return list_certain_data, list_Mass_Storage_Device

result = list_USB_all_info()

print(result)

output:

idVendor  idProduct  Manufacturer - Product
    1133      49271  Logitech - USB Optical Mouse
    1423      37728   - USB Reader
   34148       4096  JetFlash - Mass Storage Device
    7531          2  Linux 5.15.0-48-generic xhci-hcd - xHCI Host Controller
    7531          3  Linux 5.15.0-48-generic xhci-hcd - xHCI Host Controller
    5117       2112  Generic - External
    7531          2  Linux 5.15.0-48-generic xhci-hcd - xHCI Host Controller
    7531          3  Linux 5.15.0-48-generic xhci-hcd - xHCI Host Controller
Traceback (most recent call last):
  File "/home/joy/fe_dir/10_6_list_idvender_idProduct.py", line 34, in <module>
    result = list_USB_all_info()
  File "/home/joy/fe_dir/10_6_list_idvender_idProduct.py", line 26, in list_USB_all_info
    if "Mass Storage Device" in str:
TypeError: argument of type 'type' is not iterable

I did myself that I can save all output result in "all_in_string" first, then split() it, and regular expression to answer


import usb.core
import usb.backend.libusb1


def list_USB_all_info():
    print ( 'idVendor  idProduct  Manufacturer - Product')
    
    busses = usb.busses()
    
    all_in_string = ' '
    for bus in busses:
        for dev in bus.devices:
            if dev:
                xdev = usb.core.find(idVendor=dev.idVendor, idProduct=dev.idProduct)
                if xdev._manufacturer is None:
                    xdev._manufacturer = usb.util.get_string(xdev, xdev.iManufacturer)
                if xdev._product is None:
                    xdev._product = usb.util.get_string(xdev, xdev.iProduct)
                print ('%8d  %9d  %s - %s' % (dev.idVendor, dev.idProduct,
                                            str(xdev._manufacturer).strip(),
                                            str(xdev._product).strip()))
                                            
                A = ('%8d  %9d  %s - %s' % (dev.idVendor, dev.idProduct,
                                            str(xdev._manufacturer).strip(),
                                            str(xdev._product).strip()))
       
                all_in_string += A  + "/end/"+"\n" 
                         
    print ("= = = =")
    
    #if "Mass Storage Device" in str:
    #   print('that Mass Storage Device line info')
    
    #list_certain_data = "Mass Storage Device is  : "
    #list_Mass_Storage_Device = (str(dev.idVendor) + "  "+ str(dev.idProduct)+ "  |  "+str(xdev._manufacturer)+ "  |  "+str(xdev._product))
    
    return all_in_string

result = list_USB_all_info()

print(result)



j ton
  • 229
  • 9
  • 2
    Warning! Do not use `str` as a variable, as it will violate Python's `str` method, use `string` instead. You can also use [Pandas](https://pandas.pydata.org/) in this case. – Coderio Oct 06 '22 at 02:15

1 Answers1

1

I come up myself below:


import usb.core
import usb.backend.libusb1

def list_USB_all_info():
    print ( 'idVendor  idProduct  Manufacturer - Product')
    
    busses = usb.busses()
    
    all_in_string = ' '
    for bus in busses:
        for dev in bus.devices:
            if dev:
                xdev = usb.core.find(idVendor=dev.idVendor, idProduct=dev.idProduct)
                if xdev._manufacturer is None:
                    xdev._manufacturer = usb.util.get_string(xdev, xdev.iManufacturer)
                if xdev._product is None:
                    xdev._product = usb.util.get_string(xdev, xdev.iProduct)
                print ('%8d  %9d  %s - %s' % (dev.idVendor, dev.idProduct,
                                            str(xdev._manufacturer).strip(),
                                            str(xdev._product).strip()))
                                            
                A = ('%8d  %9d  %s - %s' % (dev.idVendor, dev.idProduct,
                                            str(xdev._manufacturer).strip(),
                                            str(xdev._product).strip()))
       
                all_in_string += A  + "/end/"+"\n" 
                all_in_string_splited = all_in_string.split("/end/")
                         
  

    print ("= = = =")
    all_in_string_match_Mass_Storage_Device = [s for s in all_in_string_splited if "Mass Storage Device" in s]
       
    return all_in_string_match_Mass_Storage_Device

result = list_USB_all_info()

print(result)
j ton
  • 229
  • 9