我有一个如下的字符串:
[Base Font : IOHLGA+Trebuchet, Font Size : 3.5324998, Font Weight : 0.0] [(X=250.44,Y=223.48499) height=3.5324998 width=4.2910004]DECEMBER 31,
[Base Font : IOFOEO+Imago-Book, Font Size : 3.876, Font Weight : 0.0] [(X=307.5,Y=240.48499) height=3.876 width=2.9970093]respectively. The net decrease in the revenue
[Base Font : IOHLGA+Trebuchet, Font Size : 3.5324998, Font Weight : 0.0] [(X=49.5,Y=233.98499) height=3.5324998 width=2.5690002](US$ in millions)
我希望提取“X”值及其相关文本,并将其转换为列表。请参见以下预期输出:
预期输出:
['X=250.44','DECEMBER 31,']
['X=307.5','respectively. The net decrease in the revenue']
['X=49.5','(US$ in millions)']
我们如何在Python中实现这一点?
我的方法:
mylist = []
for line in data.split("\n"):
if line.strip():
x_coord = re.findall('^(X=.*)\,$', line)
text = re.findall('^(]\w +)', line)
mylist.append([x_coord, text])
我的方法没有为x_coord
和text
识别出任何值。
str.split
函数或Python的正则表达式库。 - D Malanre
的方式。 - Jan Stránský