当某些条目需要从其他位置拆分时,如何按连续空格拆分

傻胖猫

我有以下内容,并且尝试将其拆分为Key:值字典,其中的键是协议+版本,值是是否受支持。

['SSLv2      not offered (OK)',
 'SSLv3      not offered (OK)',
 'TLS 1      not offered',
 'TLS 1.1    not offered',
 'TLS 1.2    offered (OK)',
 'TLS 1.3    not offered and downgraded to a weaker protocol',
 'NPN/SPDY   h2, http/1.1 (advertised)',
 'ALPN/HTTP2 h2, http/1.1 (offered)']

我的问题是我无法找到一种巧妙的方式来拆分此内容,由于数据使用空格,因此无法按制表符进行拆分,而且由于某些协议也具有空格,因此无法按空格进行拆分。例:

re.split(r'\s+', entries.strip(), 1)
['SSLv2', 'not offered (OK)'], ['SSLv3', 'not offered (OK)'], ['TLS', '1      not offered'], ['TLS', '1.1    not offered'], ['TLS', '1.2    offered (OK)'], ['TLS', '1.3    not offered and downgraded to a weaker protocol'], ['NPN/SPDY', 'h2, http/1.1 (advertised)'], ['ALPN/HTTP2', 'h2, http/1.1 (offered)']]

如您所见,除了某些协议,它们都可以正常工作: ['ALPN/HTTP2', 'h2, http/1.1 (offered)']

原始数据如下所示:

SSLv2      not offered (OK)
SSLv3      not offered (OK)
TLS 1      not offered
TLS 1.1    not offered
TLS 1.2    offered (OK)
TLS 1.3    not offered and downgraded to a weaker protocol
NPN/SPDY   h2, http/1.1 (advertised)
ALPN/HTTP2 h2, http/1.1 (offered)

我希望最终输出看起来像这样:

{
    'SSLv2': 'not offered (OK)',
    'SSLv3': 'not offered (OK)',
    'TLS 1': 'not offered',
    'TLS 1.1': 'not offered',
    'TLS 1.2': 'offered (OK)',
    'TLS 1.3': 'not offered and downgraded to a weaker protocol',
    'NPN/SPDY': 'h2, http/1.1 (advertised)',
    'ALPN/HTTP2': 'h2, http/1.1 (offered)'
}
维克多·史翠比维

您可以将以下正则表达式与一起使用re.findall

^(\S+(?:\s+\d+(?:\.\d+)*)?)\s+(.*)

请参阅regex演示细节:

  • ^ -字符串开始
  • (\S+(?:\s+\d+(?:\.\d+)*)?) -第1组:
    • \S+ -1个以上非空白字符
    • (?:\s+\d+(?:\.\d+)*)?-可选的非捕获组,匹配1+个空格,1 +个数字,然后出现0+个.和1+个数字
  • \s+ -1+空格
  • (.*) -第2组:其余的行/字符串。

参见Python演示

import re

entries = ['SSLv2      not offered (OK)', 'SSLv3      not offered (OK)', 'TLS 1      not offered', 'TLS 1.1    not offered', 'TLS 1.2    offered (OK)', 'TLS 1.3    not offered and downgraded to a weaker protocol', 'NPN/SPDY   h2, http/1.1 (advertised)', 'ALPN/HTTP2 h2, http/1.1 (offered)']
for entry in entries:
    print( dict(re.findall(r'^(\S+(?:\s+\d+(?:\.\d+)*)?)\s+(.*)', entry.strip())) )

输出:

{'SSLv2': 'not offered (OK)'}
{'SSLv3': 'not offered (OK)'}
{'TLS 1': 'not offered'}
{'TLS 1.1': 'not offered'}
{'TLS 1.2': 'offered (OK)'}
{'TLS 1.3': 'not offered and downgraded to a weaker protocol'}
{'NPN/SPDY': 'h2, http/1.1 (advertised)'}
{'ALPN/HTTP2': 'h2, http/1.1 (offered)'}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何拆分某些字符而不拆分其他字符?

需要从字符串动态拆分某些内容

打开拆分时如何获取vim以保留光标位置

如何将数据按一列拆分,并使用其他信息构成列?

JOLT:需要拆分数组并结合其他细节

使用Smooks或其他元素按元素拆分XML文件

按两列拆分行并保持其他列相同

表列按值拆分为其他列及其值

按空格拆分合并列...但某些数据在值之间有空格

拆分时如何刷新Vim布局

如何按空格和单词列表拆分字符串

将文本粘贴到Excel中而不拆分空格(或其他定界符)

如何在不使用连续定界符空格的情况下拆分字符串

如何按空格拆分一行以使字符串不拆分为每个字符?

如何乘车或忽略nans?使用拆分时的错误浮动

从Boost库拆分时如何添加两个值

拆分时如何查找数组最大值

itextpdf:表格拆分时如何插入新页面?

如何按列拆分DataFrame

在pandas数据框中,需要拆分列并将其添加回其他行

使用 tidyverse 按组将变量划分/拆分/分解为其他变量

通过保留单词“-”来拆分字符串,同时在Java中的其他位置将其消除

将值拆分为其他几列

在字符串被该字符拆分时如何忽略拆分模式

如何在数据帧单元中拆分数据并在拆分时执行Pandas groupby?

有没有办法在R中使其他变量保持不变的同时拆分时间和持续时间变量?

如何按空格拆分字符串并删除非 ASCII 字符?

如何在Power Query中按第一个空格拆分列?

如何在 TSQL 中按空格字符拆分字符串