Python 将字符串表转换为字典

Python 将字符串表转换为字典,python,string,dictionary,response,Python,String,Dictionary,Response,字符串中有一个表,我想将其转换为字典字典。我怎么做 我曾尝试通过拆分将字符串转换为列表,但没有得到它 dbresponse='' 密钥|帐户| guid |激活|代码|外部| id |位置 --------+--------------+-----------------+-------------+----------- 1.1 |空| 100005-1212 | 1 | 10.0.9.16 1.4 |空| 500000551212 | 5 | 10.0.9.16 1.6 |空| 700000

字符串中有一个表,我想将其转换为字典字典。我怎么做


我曾尝试通过拆分将字符串转换为列表,但没有得到它

dbresponse=''
密钥|帐户| guid |激活|代码|外部| id |位置
--------+--------------+-----------------+-------------+-----------
1.1 |空| 100005-1212 | 1 | 10.0.9.16
1.4 |空| 500000551212 | 5 | 10.0.9.16
1.6 |空| 700000551212 | 7 | 10.0.9.16
1.5 |空| 400000551212 | 4 | 10.0.9.16
1.1992 |空| 1579023555 | exr498680 | 10.0.9.16
1.3 |空| 200000551212 | 2 | 10.0.9.16
1.2052 |空| 4238550909 | exr084213 | 10.0.9.16
1.2152 |空| 563626550909 | exr350970 | 10.0.9.16
1.1534 |空| 835749550909 | exr245191 | 10.0.9.16
1.161 |空| 547489550909 | exr413464 | 10.0.9.16
1.1955 |空| 961459478950 | exr874895 | 10.0.9.16
1.1812 |空| 535999550909 | exr991462 | 10.0.9.16
1.2153 |空| 525874550909 | exr446117 | 10.0.9.16
1.2 |空| 300000551212 | 3 | 10.0.9.16
1.206 |空| 997141550909 | exr987098 | 10.0.9.16
1.101 | null | 870827550909 | exr867333 | 10.0.9.16
1.302 | null | 938271123405 | exr989961 | 10.0.9.16
1.1795 |空| 360276365614 | exr498651 | 10.0.9.16
1.855 |空| 751409654321 | exr130325 | 10.0.9.16
1.1232 |空| 397846550909 | exr557906 | 10.0.9.16
1.8 |空| 800000551111 | 8 | 10.0.9.16
1.2072 |空| 551260550909 | exr531801 | 10.0.9.16
1.9 |空| 900000551111 | 9 | 10.0.9.16
1.2092 |空| 379419235555 | exr993899 | 10.0.9.16
1.2154 |空| 916479555555 | exr465158 | 10.0.9.16
'''
这是一个作为表的python字符串。我希望输出如下:

{{'1.1':{'activation\u code':'100005-1212','account\u guid':'null',
'external_id':'1','location':'10.0.9.16'},
{'1.4':{'activation_code':'500000551212','account_guid':'null',
'external_id':'5','location':'10.0.9.16'}..}
工作示例:

import re
import pprint

db_response = '''
 key    | account_guid | activation_code | external_id | location
--------+--------------+-----------------+-------------+-----------
    1.1 |         null |    1000005-1212 |           1 | 10.0.9.16
    1.4 |         null |    500000551212 |           5 | 10.0.9.16
    1.6 |         null |    700000551212 |           7 | 10.0.9.16
    1.5 |         null |    400000551212 |           4 | 10.0.9.16
 1.1992 |         null |    157990235555 |   exr498680 | 10.0.9.16
    1.3 |         null |    200000551212 |           2 | 10.0.9.16
 1.2052 |         null |    423838550909 |   exr084213 | 10.0.9.16
 1.2152 |         null |    563626550909 |   exr350970 | 10.0.9.16
 1.1534 |         null |    835749550909 |   exr245191 | 10.0.9.16
  1.161 |         null |    547489550909 |   exr413464 | 10.0.9.16
 1.1955 |         null |    961459478950 |   exr874895 | 10.0.9.16
 1.1812 |         null |    535999550909 |   exr991462 | 10.0.9.16
 1.2153 |         null |    525874550909 |   exr446117 | 10.0.9.16
    1.2 |         null |    300000551212 |           3 | 10.0.9.16
  1.206 |         null |    997141550909 |   exr987098 | 10.0.9.16
  1.101 |         null |    870827550909 |   exr867333 | 10.0.9.16
  1.302 |         null |    938271123405 |   exr989961 | 10.0.9.16
 1.1795 |         null |    360276365614 |   exr498651 | 10.0.9.16
  1.855 |         null |    751409654321 |   exr130325 | 10.0.9.16
 1.1232 |         null |    397846550909 |   exr557906 | 10.0.9.16
    1.8 |         null |    800000551111 |           8 | 10.0.9.16
 1.2072 |         null |    551260550909 |   exr531801 | 10.0.9.16
    1.9 |         null |    900000551111 |           9 | 10.0.9.16
 1.2092 |         null |    379419235555 |   exr993899 | 10.0.9.16
 1.2154 |         null |    916479555555 |   exr465158 | 10.0.9.16
'''

lines = [re.sub(r'\s+', '', line).split('|') for line in db_response.split('\n') if line and '-----' not in line]
head = lines[0]
result = {}
for line in lines[1:]:
    result[line[0]] = {col:line[no] for no, col in enumerate(head[1:])}
pprint.pprint(result)
输出:

pawel@pawel-XPS-15-9570:~/test$ python parse.py 
{'1.1': {'account_guid': '1.1',
         'activation_code': 'null',
         'external_id': '1000005-1212',
         'location': '1'},
 '1.101': {'account_guid': '1.101',
           'activation_code': 'null',
           'external_id': '870827550909',
           'location': 'exr867333'},
 '1.1232': {'account_guid': '1.1232',
            'activation_code': 'null',
            'external_id': '397846550909',
            'location': 'exr557906'},
 '1.1534': {'account_guid': '1.1534',
            'activation_code': 'null',
            'external_id': '835749550909',
            'location': 'exr245191'},
 '1.161': {'account_guid': '1.161',
           'activation_code': 'null',
           'external_id': '547489550909',
           'location': 'exr413464'},
 '1.1795': {'account_guid': '1.1795',
            'activation_code': 'null',
            'external_id': '360276365614',
            'location': 'exr498651'},
 '1.1812': {'account_guid': '1.1812',
            'activation_code': 'null',
            'external_id': '535999550909',
            'location': 'exr991462'},
 '1.1955': {'account_guid': '1.1955',
            'activation_code': 'null',
            'external_id': '961459478950',
            'location': 'exr874895'},
 '1.1992': {'account_guid': '1.1992',
            'activation_code': 'null',
            'external_id': '157990235555',
            'location': 'exr498680'},
 '1.2': {'account_guid': '1.2',
         'activation_code': 'null',
         'external_id': '300000551212',
         'location': '3'},
 '1.2052': {'account_guid': '1.2052',
            'activation_code': 'null',
            'external_id': '423838550909',
            'location': 'exr084213'},
 '1.206': {'account_guid': '1.206',
           'activation_code': 'null',
           'external_id': '997141550909',
           'location': 'exr987098'},
 '1.2072': {'account_guid': '1.2072',
            'activation_code': 'null',
            'external_id': '551260550909',
            'location': 'exr531801'},
 '1.2092': {'account_guid': '1.2092',
            'activation_code': 'null',
            'external_id': '379419235555',
            'location': 'exr993899'},
 '1.2152': {'account_guid': '1.2152',
            'activation_code': 'null',
            'external_id': '563626550909',
            'location': 'exr350970'},
 '1.2153': {'account_guid': '1.2153',
            'activation_code': 'null',
            'external_id': '525874550909',
            'location': 'exr446117'},
 '1.2154': {'account_guid': '1.2154',
            'activation_code': 'null',
            'external_id': '916479555555',
            'location': 'exr465158'},
 '1.3': {'account_guid': '1.3',
         'activation_code': 'null',
         'external_id': '200000551212',
         'location': '2'},
 '1.302': {'account_guid': '1.302',
           'activation_code': 'null',
           'external_id': '938271123405',
           'location': 'exr989961'},
 '1.4': {'account_guid': '1.4',
         'activation_code': 'null',
         'external_id': '500000551212',
         'location': '5'},
 '1.5': {'account_guid': '1.5',
         'activation_code': 'null',
         'external_id': '400000551212',
         'location': '4'},
 '1.6': {'account_guid': '1.6',
         'activation_code': 'null',
         'external_id': '700000551212',
         'location': '7'},
 '1.8': {'account_guid': '1.8',
         'activation_code': 'null',
         'external_id': '800000551111',
         'location': '8'},
 '1.855': {'account_guid': '1.855',
           'activation_code': 'null',
           'external_id': '751409654321',
           'location': 'exr130325'},
 '1.9': {'account_guid': '1.9',
         'activation_code': 'null',
         'external_id': '900000551111',
         'location': '9'}}
输出

[{'    1.1 ': {'account_guid': '         null ',
   'activation_code': '    1000005-1212 ',
   'external_id': '           1 ',
   'location': ' 10.0.9.16'}},
 {'    1.4 ': {'account_guid': '         null ',
   'activation_code': '    500000551212 ',
   'external_id': '           5 ',
   'location': ' 10.0.9.16'}},
 {'    1.6 ': {'account_guid': '         null ',
   'activation_code': '    700000551212 ',
   'external_id': '           7 ',
   'location': ' 10.0.9.16'}},
 {'    1.5 ': {'account_guid': '         null ',
   'activation_code': '    400000551212 ',
   'external_id': '           4 ',
   'location': ' 10.0.9.16'}},
 {' 1.1992 ': {'account_guid': '         null ',
   'activation_code': '    157990235555 ',
   'external_id': '   exr498680 ',
   'location': ' 10.0.9.16'}}]
提示: 1.把绳子分成几行
2.将行拆分为列

您需要拆分每一行,然后处理字段,然后一次生成一行字典

注意:删除了
dbResponse
声明以消除任何滚动

重新导入
''将表转换为对象字典
Args:
表(字符串):表数据
键(字符串):主键
分隔符(regex):用于分隔行的可选分隔符
返回:
结果:表的字典表示形式,使用主键
'''
def table_to_dict(table,key,delimiter='\s*\\\s*'):
结果={}#初始化字典(对象)
lines=table.strip().split('\n')#拆分新行
fields=re.split(分隔符,行[0])#字段
keyIndex=字段。索引(键)#主键的索引
对于行中的行[2:]:#从第三行开始
数据=重新拆分(分隔符,行)#拆分每行
结果[data[keyIndex]={}#在字典中创建一个新条目
对于i,枚举(数据)中的值:#枚举值
如果我keyIndex:#不要将键值存储在dict中
结果[data[keyIndex]][fields[i]]=value#在dict的条目中设置字段的值
返回结果#返回结果
#主输入功能
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
打印(表_to _dict(dbResponse,'key'))#其中'dbResponse'是上述数据
结果
{'1.1':{'account_guid':'null',activation_code':'100005-1212','external_id':'1','location':'10.0.9.16','1.4':{'account_guid':'null','activation_code':'500000551212','external_id':'5','location':'10.0.9.16','1.6','account guid':'null','activation_code':'7000055212','external id':'7','location':'10.9.16'','1.5':{'account_guid':'null','activation_code':'400000551212','external_id':'4','location':'10.0.9.16'},'1.1992':{'account_guid':'null','activation_code':'15790023555','external_id':'exr498680','location':'10.0.9.16'},'1.3':{'account_guid':'null','activation_code':'200000551212','external_id':'2','location':'10.0.9.16'},'1.2052':{'account_guid':'null','activation_code':'4238550909','external_id':'exr084213','location':'10.0.9.16'},'1.2152':{'account_guid':'null','activation_code':'563626550909','external_id':'exr350970','location':'10.0.9.16'},'1.1534':{'account_guid':'null','activation_code':'835749550909','external_id':'exr245191','location':'10.0.9.16'},'1.161':{'account_guid':'null','activation_code':'547489550909','external_id':'exr413464','location':'10.0.9.16'},'1.1955':{'account_guid':'null','activation_code':'961459478950','external_id':'exr874895','location':'10.0.9.16'},'1.1812':{'account_guid':'null','activation_code':'535999550909','external_id':'exr991462','location':'10.0.9.16'},'1.2153':{'account_guid':'null','activation_code':'525874550909','external_id':'exr446117','location':'10.0.9.16'},'1.2':{'account_guid':'null','activation_code':'300000551212','external_id':'3','location':'10.0.9.16'},'1.206':{'account_guid':'null','activation_code':'997141550909','external_id':'exr987098','location':'10.0.9.16'},'1.101':{'account gui
[{'    1.1 ': {'account_guid': '         null ',
   'activation_code': '    1000005-1212 ',
   'external_id': '           1 ',
   'location': ' 10.0.9.16'}},
 {'    1.4 ': {'account_guid': '         null ',
   'activation_code': '    500000551212 ',
   'external_id': '           5 ',
   'location': ' 10.0.9.16'}},
 {'    1.6 ': {'account_guid': '         null ',
   'activation_code': '    700000551212 ',
   'external_id': '           7 ',
   'location': ' 10.0.9.16'}},
 {'    1.5 ': {'account_guid': '         null ',
   'activation_code': '    400000551212 ',
   'external_id': '           4 ',
   'location': ' 10.0.9.16'}},
 {' 1.1992 ': {'account_guid': '         null ',
   'activation_code': '    157990235555 ',
   'external_id': '   exr498680 ',
   'location': ' 10.0.9.16'}}]