Python查找字符串中的文本

2024-04-29 16:59:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下字符串,要为其提取数据:

text_example = '\nExample text \nTECHNICAL PARTICULARS\nLength oa: ...............189.9m\nLength bp: ........176m\nBreadth moulded:  .......26.4m\nDepth moulded to main deck:  ....9.2m\n
  • 我要提取的每个变量都以开头\n
  • 我要获取的值以冒号“:”开头,后跟超过1个点
  • 当它不是以冒号和点开头时,我不想提取那个值

例如,我的首选输出如下所示:

LOA = 189.9
LBP = 176.0
BM = 26.4
DM = 9.2

Tags: to数据字符串textexamplebpoa冒号
2条回答
import re

text_example = '\nExample text \nTECHNICAL PARTICULARS\nLength oa: ...............189.9m\nLength bp: ........176m\nBreadth moulded:  .......26.4m\nDepth moulded to main deck:  ....9.2m\n'

# capture all the characters BEFORE the ':' character

variables = re.findall(r'(.*?):', text_example)

# matches all floats and integers (does not account for minus signs)

values = re.findall(r'(\d+(?:\.\d+)?)', text_example)

# zip into dictionary (this is assuming you will have the same number of results for both regex expression.

result = dict(zip(variables, values))

print(result)

>;{‘长度oa’:‘189.9’,‘宽度模制’:‘26.4’,‘长度bp’:‘176’,‘深度模制至主甲板’:‘9.2’}

您可以创建一个正则表达式和解决方案-

re.findall(r'(\\n|\n)([A-Za-z\s]*)(?:(\:\s*\.+))(\d*\.*\d*)',text_example)[2]

('\n', 'Breadth moulded', ':  .......', '26.4')    

                                                

相关问题 更多 >