一些键值对转字典或表格(实在是想不出来了,不是那种标准格式的键值对,求大佬给个思路)

一些键值对转字典或表格(实在是想不出来了,不是那种标准格式的键值对)
如上图,a没有值(不只是第一个键没有值,后边的键不一定有没有值),距离b两个空格,
后续的b和c都有值,且与值之间两个空格,
但是也有不听话的(d),有值但中间三个空格
需要处理成:
a:
b:1
c:2
d:3
类似的就可以,主要是键值对出来就可以了
最后附上类似字符串:
a: b: 1 c: 2 d: 3

Jason990420
最佳答案

正则式最重要的就是找出问题的规则, 原问题与后面再提出的问题, 很明显两者的规则不一样, 也就是说你先问了一个不相关的问题, 这会浪费彼此的时间.

import re
import pprint

text = '主题: 安全 ID: NT AUTHORITY\SYSTEM 帐户名: SNAB 账户域: BIZ 登陆类型: 5 登录失败的账户: 安全ID: NULL SID'

# Split by white spaces if Chinese/English/digit characters and ':' after it.
regex1 = re.compile(r"\s+(?=[\u4e00-\u9fa5_A-Za-z0-9]+:)")
items = regex1.split(text)

# Split each item by ":" and white spaces, also the special case when no value.
regex2 = re.compile(r":\s+")
result = list(map(lambda x:regex2.split(x) if ' ' in x else [x, ''], items))

pprint.pprint(dict(result))
{'ID': 'NT AUTHORITY\\SYSTEM',
 '主题': '安全',
 '安全ID': 'NULL SID',
 '帐户名': 'SNAB',
 '登录失败的账户:': '',
 '登陆类型': '5',
 '账户域': 'BIZ'}
2年前 评论
quinn_ (楼主) 2年前
讨论数量: 7
Jason990420
import re

text = 'a:  b:  1  c:  2  d:    3'

regex = re.compile(r"""
    (\w+)   # 1st Capturing Group, matches any [a-zA-Z0-9_], between one and unlimited times
    :       # matches the character :
    \s+     # matches any [\r\n\t\f\v ], between one and unlimited times
    (\w*)   # 2nd Capturing Group, matches any [a-zA-Z0-9_], between zero and unlimited times
    (?!:)   # Negative Lookahead, Assert that the Regex below does not match
    """,
    re.VERBOSE,
)
items = regex.findall(text)
result = dict(items)
print(result)
{'a': '', 'b': '1', 'c': '2', 'd': '3'}
2年前 评论
quinn_ (楼主) 2年前
quinn_ (楼主) 2年前
Jason990420

正则式最重要的就是找出问题的规则, 原问题与后面再提出的问题, 很明显两者的规则不一样, 也就是说你先问了一个不相关的问题, 这会浪费彼此的时间.

import re
import pprint

text = '主题: 安全 ID: NT AUTHORITY\SYSTEM 帐户名: SNAB 账户域: BIZ 登陆类型: 5 登录失败的账户: 安全ID: NULL SID'

# Split by white spaces if Chinese/English/digit characters and ':' after it.
regex1 = re.compile(r"\s+(?=[\u4e00-\u9fa5_A-Za-z0-9]+:)")
items = regex1.split(text)

# Split each item by ":" and white spaces, also the special case when no value.
regex2 = re.compile(r":\s+")
result = list(map(lambda x:regex2.split(x) if ' ' in x else [x, ''], items))

pprint.pprint(dict(result))
{'ID': 'NT AUTHORITY\\SYSTEM',
 '主题': '安全',
 '安全ID': 'NULL SID',
 '帐户名': 'SNAB',
 '登录失败的账户:': '',
 '登陆类型': '5',
 '账户域': 'BIZ'}
2年前 评论
quinn_ (楼主) 2年前
sxyclys
    text = '主题: 安全 ID: NT AUTHORITY\SYSTEM 帐户名: SNAB 账户域: BIZ 登陆类型: 5 登录失败的账户: 安全ID: NULL SID'
    text_list = text.split(' ')
    target_dict = dict()
    print(text_list)
    for cu in text_list:
        if ':' in cu:
            cu_key = cu.replace(':', '')
            target_dict[cu_key] = None
        else:
            if cu != '':
                if target_dict[cu_key] is None:
                    target_dict[cu_key] = cu
                else:
                    target_dict[cu_key] = target_dict[cu_key] + ' ' + cu
    print(target_dict)

结果

{'主题': '安全', 'ID': 'NT AUTHORITY\\SYSTEM', '帐户名': 'SNAB', '账户域': 'BIZ', '登陆类型': '5', '登录失败的账户': None, '安全ID': 'NULL SID'}
2年前 评论
quinn_ (楼主) 2年前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!