Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用正则表达式提取列表中字符串的一部分_Python_Regex - Fatal编程技术网

Python 使用正则表达式提取列表中字符串的一部分

Python 使用正则表达式提取列表中字符串的一部分,python,regex,Python,Regex,与标题状态一样,我尝试使用正则表达式提取字符串的一部分,即列表中的一部分。 该列表包含多个如下所示的字符串: "[Decoded(data=b'FF01664817', rect=Rect(left=132, top=207, width=171,height=1))]", "[Decoded(data=b'FF01664833', rect=Rect(left=227, top=128, width=-6, height=175))]" 对于一些上下文,字符串是我使用cv2解码的数据矩

与标题状态一样,我尝试使用正则表达式提取字符串的一部分,即列表中的一部分。 该列表包含多个如下所示的字符串:

 "[Decoded(data=b'FF01664817', rect=Rect(left=132, top=207, width=171,height=1))]", 
 "[Decoded(data=b'FF01664833', rect=Rect(left=227, top=128, width=-6, height=175))]"
对于一些上下文,字符串是我使用
cv2
解码的数据矩阵。我想要的是得到
'
(数据矩阵内容)之间的部分,而不需要其他部分

我的方法如下:

Data=[re.match(r"\'.*'\)",x[0]) for x in Data]
但当我打印数据时,它只为列表中的每个字符串返回
“Null”

代码的其余部分

import cv2
import numpy as np
import ctypes  
from pylibdmtx.pylibdmtx import decode
import csv
import re

img = cv2.imread('C:/Users/ML/Desktop/DataMatrix/Test2.jpg')
img2 = img

height, width, channels = img.shape

CROP_W_SIZE  = 8 
CROP_H_SIZE = 6

Data = []

for ih in range(CROP_H_SIZE ):
    for iw in range(CROP_W_SIZE ):

        x = int(width / CROP_W_SIZE * iw)
        y = int(height / CROP_H_SIZE * ih)
        h = int((height / CROP_H_SIZE))
        w = int((width / CROP_W_SIZE ))
       # print(x,y,h,w)

        img = img[y:y+h, x:x+w]

        Name = str(time.time()) 
        cv2.imwrite("C:/Users/ML/Desktop/DataMatrix/CROP/" + 'Crop' + str(x+y) +  ".jpg",img)
        img = img2

        Data.append(str(decode(cv2.imread('C:/Users/ML/Desktop/DataMatrix/CROP/'+ 'Crop' + str(x+y) +'.jpg'))))

Data=[re.match(r"\'.*'\)",x[0]) for x in Data]
print(Data)
使用
search()
而不是
match()
。仅当匹配项位于字符串开头时,最后一个函数才起作用:

import re

s = "[Decoded(data=b'FF01664817', rect=Rect(left=132, top=207, width=171, height=1))]"

print(re.search(r"'(.+?)'", s).group())
# FF01664817
使用
search()
而不是
match()
。仅当匹配项位于字符串开头时,最后一个函数才起作用:

import re

s = "[Decoded(data=b'FF01664817', rect=Rect(left=132, top=207, width=171, height=1))]"

print(re.search(r"'(.+?)'", s).group())
# FF01664817

这是超级脆弱的,可能会破坏可怕的数据,看起来不像你的,但

重新导入
def解析键值:
返回{
m、 组(1):m组(2)或m组(3)
对于m in re.finditer(
r“([a-z]+)=(?:b\'(.+?)\'(.d+?)[,)]”,s
)
}
对于x英寸[
“[已解码(数据=b'FF01664817',rect=rect(左=132,顶=207,宽=171,高=1))”,
“[已解码(数据=b'FF01664833',rect=rect(左=227,顶=128,宽=6,高=175))”,
]:
打印(解析键值(x))
输出

{'data': 'FF01664817', 'left': '132', 'top': '207', 'width': '171', 'height': '1'}
{'data': 'FF01664833', 'left': '227', 'top': '128', 'width': '-6', 'height': '175'}

这是超级脆弱的,可能会破坏可怕的数据,看起来不像你的,但

重新导入
def解析键值:
返回{
m、 组(1):m组(2)或m组(3)
对于m in re.finditer(
r“([a-z]+)=(?:b\'(.+?)\'(.d+?)[,)]”,s
)
}
对于x英寸[
“[已解码(数据=b'FF01664817',rect=rect(左=132,顶=207,宽=171,高=1))”,
“[已解码(数据=b'FF01664833',rect=rect(左=227,顶=128,宽=6,高=175))”,
]:
打印(解析键值(x))
输出

{'data': 'FF01664817', 'left': '132', 'top': '207', 'width': '171', 'height': '1'}
{'data': 'FF01664833', 'left': '227', 'top': '128', 'width': '-6', 'height': '175'}
regex match()只匹配字符串的开头。 regex search()搜索所有字符串

import re
list = ["[Decoded(data=b'FF01664817', rect=Rect(left=132, top=207, width=171, height=1))]",
        "[Decoded(data=b'FF01664833', rect=Rect(left=227, top=128, width=-6, height=175))]"]
data = [re.search(r''''.*''', x) for x in list]
输出:

[<_sre.SRE_Match object; span=(15, 80), match="'FF01664817', rect=Rect(left=132, top=207, width=>, <_sre.SRE_Match object; span=(15, 81), match="'FF01664833', rect=Rect(left=227, top=128, width=>]
regex match()只匹配字符串的开头。 regex search()搜索所有字符串

import re
list = ["[Decoded(data=b'FF01664817', rect=Rect(left=132, top=207, width=171, height=1))]",
        "[Decoded(data=b'FF01664833', rect=Rect(left=227, top=128, width=-6, height=175))]"]
data = [re.search(r''''.*''', x) for x in list]
输出:

[<_sre.SRE_Match object; span=(15, 80), match="'FF01664817', rect=Rect(left=132, top=207, width=>, <_sre.SRE_Match object; span=(15, 81), match="'FF01664833', rect=Rect(left=227, top=128, width=>]

我想您正在寻找
re.search
re.findall

import re

v = ["[Decoded(data=b'FF01664817', rect=Rect(left=132, top=207, width=171, \
        height=1))]", "[Decoded(data=b'FF01664833', rect=Rect(left=227, \
        top=128, width=-6, height=175))]"]
se = [re.search(r"b'(.+)'", x).group(1) for x in v]
fa = [re.findall(r"b'(.+)'", x) for x in v]
print(se)
print(fa)
产出:

['FF01664817','FF01664833']
[['FF01664817'],['FF01664833']]


我想您正在寻找
re.search
re.findall

import re

v = ["[Decoded(data=b'FF01664817', rect=Rect(left=132, top=207, width=171, \
        height=1))]", "[Decoded(data=b'FF01664833', rect=Rect(left=227, \
        top=128, width=-6, height=175))]"]
se = [re.search(r"b'(.+)'", x).group(1) for x in v]
fa = [re.findall(r"b'(.+)'", x) for x in v]
print(se)
print(fa)
产出:

['FF01664817','FF01664833']
[['FF01664817'],['FF01664833']]


试着摆脱
str
然后就可以了

Data.extend(decode(cv2.imread('C:/Users/ML/Desktop/DataMatrix/CROP/'+ 'Crop' + str(x+y) +'.jpg')))
在循环中

然后试着做:

Data = [x.data for x in Data]
或者在循环中,您可以直接执行以下操作:

Data.extend(i.data for i in decode(cv2.imread(
    'C:/Users/ML/Desktop/DataMatrix/CROP/'+ 'Crop' + str(x+y) +'.jpg'
)))
然后,
数据
将包含您需要的内容

Decoded
是一个命名元组,具有
data
rect
属性,因此您可以直接访问
.data
并获取所需内容(您可以查看其定义)

使用正则表达式提取您需要的东西是缓慢的、不可靠的,而且相当笨拙

通过直接对对象进行操作,您可以更灵活地编写列表和传递列表


您还可以保留属性的原始类型。

尝试摆脱
str
,只需

Data.extend(decode(cv2.imread('C:/Users/ML/Desktop/DataMatrix/CROP/'+ 'Crop' + str(x+y) +'.jpg')))
在循环中

然后试着做:

Data = [x.data for x in Data]
或者在循环中,您可以直接执行以下操作:

Data.extend(i.data for i in decode(cv2.imread(
    'C:/Users/ML/Desktop/DataMatrix/CROP/'+ 'Crop' + str(x+y) +'.jpg'
)))
然后,
数据
将包含您需要的内容

Decoded
是一个命名元组,具有
data
rect
属性,因此您可以直接访问
.data
并获取所需内容(您可以查看其定义)

使用正则表达式提取您需要的东西是缓慢的、不可靠的,而且相当笨拙

通过直接对对象进行操作,您可以更灵活地编写列表和传递列表


您还保留了属性的原始类型。

您试图获得什么作为输出?这些数据看起来像python对象的字符串表示,这就引出了一个问题:为什么您试图解析字符串而不是直接使用这些对象?我认为您这样做是错误的。。。如果可以再次运行生成该输出的程序,则应将其更改为机器可读格式(如JSON)的输出文件,而不是尝试解析这些repr。FF01664817、FF01664833如何直接使用这些对象?您试图获得的输出是什么?这些数据看起来像python对象的字符串表示形式,这就引出了一个问题,为什么你试图解析一个字符串而不是直接使用那些对象?我认为你这样做是错误的。。。如果您可以再次运行生成该输出的程序,您应该将其更改为机器可读格式(如JSON)的输出文件,而不是尝试解析这些repr。FF01664817,FF01664833如何直接使用这些对象?嗯,尝试了此方法,但我总是遇到错误:“list”对象没有属性“data”啊,是的,对不起,
decode
返回一个列表,你能看到我的编辑吗?嗯,我尝试了这个方法,但总是出现错误:“list”对象没有属性“data”啊,是的,对不起,
decode
返回一个列表,你能看到我的编辑吗?