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条回答
Viruses.
2楼-- · 2019-04-17 08:38

Use regular expressions:

import re
p =re.compile(r'.*UID(\d+)')
with open('infile') as infile:
    for line in infile:
        m = p.match(line)
        if m:
           print m.groups[0]
查看更多
成全新的幸福
3楼-- · 2019-04-17 08:38
>>> for line in s.splitlines():
...     line = line.strip()
...     if "UID" in line:
...             tmp = line.split("UID")
...             uid = tmp[1].split(':')[0]
...             print "UID " + uid
... 
UID 68092928                     
UID 51249920
查看更多
三岁会撩人
4楼-- · 2019-04-17 08:42

I would use a regular expresssion to extract the UID

e.g.

import re

regexp = re.compile('UID(\d+)')

file = """[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."""

print re.findall(regexp, file)
查看更多
小情绪 Triste *
5楼-- · 2019-04-17 08:43

You can use the split() method.

s = "hello this is a test"
words = s.split(" ")
print words

The output of the above snippet is a list containing: ['hello', 'this', 'is', 'a', 'test']

In your case, you can split on the substring "UID" and grab the second element in the list to get the number that you're looking for.

See docs here: https://docs.python.org/2/library/string.html#string.split

查看更多
Explosion°爆炸
6楼-- · 2019-04-17 08:44

This is a bit esoteric but does the trick with some list comprehension:

[this.split("UID")[1].split()[0] for this in txt.split("\n") if "UID" in this]

the output is the list you are looking for I presume: ['68092928', '51249920']

Explanations:

  1. split the text into rows (split("\n")
  2. select only rows with UID inside (for this in ... if "UID" in this)
  3. in the remaining rows, split using "UID".
  4. You want to keep only one element after UID hence the [1]
  5. The resulting string contains the id and some text separated by a space so, we use a second split(), defaulting to spaces.
查看更多
7楼-- · 2019-04-17 08:44

if you read the whole file at once, otherwise if line by line just change the first line to line.split()

for elem in file.split():
    if 'UID' in elem:
        print elem.split('UID')[1]

the split will have already stripped "junk" do each elem that contains the 'UID' string will be all set to int() or just print as a string

查看更多
登录 后发表回答