【发布时间】:2019-03-08 10:43:18
【问题描述】:
如标题所述,我正在尝试使用正则表达式来提取字符串的一部分,即列表中的一部分。 该列表包含多个字符串,如下所示:
"[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)
【问题讨论】:
-
你想得到什么输出?
-
该数据看起来像 python 对象的字符串表示形式,这就引出了一个问题,为什么您要尝试解析字符串而不是直接使用这些对象?
-
我认为您的做法是错误的...如果您可以再次运行生成该输出的程序,则应将其更改为以机器可读格式(如 JSON)输出文件试图解析那些代表。
-
FF01664817, FF01664833
-
如何直接使用对象?