python - How to extract strings from each line in

2019-04-17 08:10发布

I have a text file that detects the amount of monitors that are active. I want to extract specific data from each line and include it in a list.

The text file looks like this:

[EnumerateDevices]: Enumerating Devices.
DISPLAY\LGD03D7\4&ACE0355&1&UID68092928                     : Generic PnP Monitor
DISPLAY\ABCF206\4&ACE0355&1&UID51249920                     : Generic PnP Monitor
//
//   here can be more monitors...
//
2 matching device(s) found.

I need to get the number after the UID in the middle of the text : 68092929 , 51249920 ..

I thought of doing the next:

a. enter each line in text

b. see if "UID" string exist

c. if it exists : split (here I dot know how to do it.. split by (" ") or ("&")

Is there any good idea you can advise? I don't understand how can I get the numbers after the UID (if the next number is longer than the previous ones for example) how can I get a command that does : ("If you see UID string, get all the data until you see first blank")

any idea? Thanks

7条回答
我只想做你的唯一
2楼-- · 2019-04-17 08:46

You can use the find() method:

if line.find('UID') != -1:
    print line[line.find('UID') + 2 :]

Docs https://docs.python.org/2/library/string.html#string.find

查看更多
登录 后发表回答