将字符串 table 转换为字典
convert string table into dictionary
我在字符串中有一个table,我想把它转换成字典的字典。我该怎么做?
我试过通过拆分将字符串转换为列表,但我没有成功。
dbresponse = '''
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
'''
这是一个 python 字符串作为 table。我想要这样的输出:
{{'1.1' : {'activation_code': '1000005-1212', 'account_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'}}
dbResponse = '''
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
'''
split_str = dbResponse.split('\n')
split_str = [s for s in split_str if len(s) > 0]
strip_str = lambda x: x.strip()
column = list(map(strip_str,split_str[0].split('|')))
outresult = []
for clm in split_str[2:]:
outdict = {}
out = {}
splited_clm = clm.split("|")
i = 1
for c in column[1:]:
out[c] = splited_clm[i]
i = i + 1
outdict[splited_clm[0]] = out
outresult.append(outdict)
输出
[{' 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
声明以消除任何滚动。
import re
'''Converts a table to an object dictionary
Args:
table (string) : The table data
key (string) : The primary key
delimiter (regex) : An Optional delimiter used for splitting lines
Returns:
result: A dictionary representation of the table, using the primary key
'''
def table_to_dict(table, key, delimiter='\s*\|\s*'):
result = {} # Initialize dictionary (object)
lines = table.strip().split('\n') # Split new-lines
fields = re.split(delimiter, lines[0]) # The fields
keyIndex = fields.index(key) # Index of the primary key
for line in lines[2:]: # Start on the third line
data = re.split(delimiter, line) # Split each line
result[data[keyIndex]] = {} # Create a new entry in the dictionary
for i, value in enumerate(data): # Enumerate over the values
if i != keyIndex: # Do not store the key-value within the dictt
result[data[keyIndex]][fields[i]] = value # Set the value for they field in the entry of the dict
return result # Return the result
# Main entry function
if __name__ == '__main__':
print(table_to_dict(dbResponse, 'key')) # Where `dbResponse` is the data above
结果
{' 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.3': {'account_guid': 'null', 'activation_code': '200000551212', 'external_id': '2', 'location': '10.0.9.16'}, ' 1.2052': {'account_guid': 'null', 'activation_code': '423838550909', '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_guid': 'null', 'activation_code': '870827550909', 'external_id': 'exr867333', 'location': '10.0.9.16'}, ' 1.302': {'account_guid': 'null', 'activation_code': '938271123405', 'external_id': 'exr989961', 'location': '10.0.9.16'}, ' 1.1795': {'account_guid': 'null', 'activation_code': '360276365614', 'external_id': 'exr498651', 'location': '10.0.9.16'}, ' 1.855': {'account_guid': 'null', 'activation_code': '751409654321', 'external_id': 'exr130325', 'location': '10.0.9.16'}, ' 1.1232': {'account_guid': 'null', 'activation_code': '397846550909', 'external_id': 'exr557906', 'location': '10.0.9.16'}, ' 1.8': {'account_guid': 'null', 'activation_code': '800000551111', 'external_id': '8', 'location': '10.0.9.16'}, ' 1.2072': {'account_guid': 'null', 'activation_code': '551260550909', 'external_id': 'exr531801', 'location': '10.0.9.16'}, ' 1.9': {'account_guid': 'null', 'activation_code': '900000551111', 'external_id': '9', 'location': '10.0.9.16'}, ' 1.2092': {'account_guid': 'null', 'activation_code': '379419235555', 'external_id': 'exr993899', 'location': '10.0.9.16'}, ' 1.2154': {'account_guid': 'null', 'activation_code': '916479555555', 'external_id': 'exr465158', 'location': '10.0.9.16'}}
我在字符串中有一个table,我想把它转换成字典的字典。我该怎么做?
我试过通过拆分将字符串转换为列表,但我没有成功。
dbresponse = '''
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
'''
这是一个 python 字符串作为 table。我想要这样的输出:
{{'1.1' : {'activation_code': '1000005-1212', 'account_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'}}
dbResponse = '''
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
'''
split_str = dbResponse.split('\n')
split_str = [s for s in split_str if len(s) > 0]
strip_str = lambda x: x.strip()
column = list(map(strip_str,split_str[0].split('|')))
outresult = []
for clm in split_str[2:]:
outdict = {}
out = {}
splited_clm = clm.split("|")
i = 1
for c in column[1:]:
out[c] = splited_clm[i]
i = i + 1
outdict[splited_clm[0]] = out
outresult.append(outdict)
输出
[{' 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
声明以消除任何滚动。
import re
'''Converts a table to an object dictionary
Args:
table (string) : The table data
key (string) : The primary key
delimiter (regex) : An Optional delimiter used for splitting lines
Returns:
result: A dictionary representation of the table, using the primary key
'''
def table_to_dict(table, key, delimiter='\s*\|\s*'):
result = {} # Initialize dictionary (object)
lines = table.strip().split('\n') # Split new-lines
fields = re.split(delimiter, lines[0]) # The fields
keyIndex = fields.index(key) # Index of the primary key
for line in lines[2:]: # Start on the third line
data = re.split(delimiter, line) # Split each line
result[data[keyIndex]] = {} # Create a new entry in the dictionary
for i, value in enumerate(data): # Enumerate over the values
if i != keyIndex: # Do not store the key-value within the dictt
result[data[keyIndex]][fields[i]] = value # Set the value for they field in the entry of the dict
return result # Return the result
# Main entry function
if __name__ == '__main__':
print(table_to_dict(dbResponse, 'key')) # Where `dbResponse` is the data above
结果
{' 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.3': {'account_guid': 'null', 'activation_code': '200000551212', 'external_id': '2', 'location': '10.0.9.16'}, ' 1.2052': {'account_guid': 'null', 'activation_code': '423838550909', '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_guid': 'null', 'activation_code': '870827550909', 'external_id': 'exr867333', 'location': '10.0.9.16'}, ' 1.302': {'account_guid': 'null', 'activation_code': '938271123405', 'external_id': 'exr989961', 'location': '10.0.9.16'}, ' 1.1795': {'account_guid': 'null', 'activation_code': '360276365614', 'external_id': 'exr498651', 'location': '10.0.9.16'}, ' 1.855': {'account_guid': 'null', 'activation_code': '751409654321', 'external_id': 'exr130325', 'location': '10.0.9.16'}, ' 1.1232': {'account_guid': 'null', 'activation_code': '397846550909', 'external_id': 'exr557906', 'location': '10.0.9.16'}, ' 1.8': {'account_guid': 'null', 'activation_code': '800000551111', 'external_id': '8', 'location': '10.0.9.16'}, ' 1.2072': {'account_guid': 'null', 'activation_code': '551260550909', 'external_id': 'exr531801', 'location': '10.0.9.16'}, ' 1.9': {'account_guid': 'null', 'activation_code': '900000551111', 'external_id': '9', 'location': '10.0.9.16'}, ' 1.2092': {'account_guid': 'null', 'activation_code': '379419235555', 'external_id': 'exr993899', 'location': '10.0.9.16'}, ' 1.2154': {'account_guid': 'null', 'activation_code': '916479555555', 'external_id': 'exr465158', 'location': '10.0.9.16'}}