我有一个保存为txt文件的XML文件,我正试图从中提取数据以使用Python显示它

2024-09-24 00:24:02 发布

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

数据的格式为“25.91” 这样我有多行

我正在尝试提取数字字符数据25.91 25.91

我已经尝试过为此编写代码,但有些东西就是不起作用

import re
myfile = open("BOMR1_.txt")
for line in myfile:
    line=line.rstrip()
    StartMP = re.findall(r'^xml.*<FROM>([0-9.]+)', line)
    if len(StartMP)> 0:
        Print (StartMP)

我想查看以下数据: Startmp=25.91,依此类推


Tags: 数据代码inimportretxtfor格式
1条回答
网友
1楼 · 发布于 2024-09-24 00:24:02

使用正则表达式解析XML等分层数据格式不是一个好主意。有关正则表达式处理器本身不在解析器中的原因的更多详细信息,请参见this fantastic article

那篇文章的关键一段是:

Regular expressions are not Parsers. Although you can do some amazing things with regular expressions, they are weak at balanced tag matching. Some regex variants have balanced matching, but it is clearly a hack – and a nasty one. You can often make it kinda-sorta work, as I have in the sanitize routine. But no matter how clever your regex, don't delude yourself: it is in no way, shape or form a substitute for a real live parser.

相关问题 更多 >