如何解析具有不同数量键值的元组列表
How to Parse a list of tuples with different number of key values
我有一个 tuples
这样的列表。
('change', ['System.Rev', 'newValue'], (1, 2))
('add', ['System.Rev'], [('oldValue', 1)])
('change', ['System.AuthorizedDate', 'newValue'], ('2021-10-27T11:10:45.047Z', '2021-10-27T11:10:45.77Z'))
('add', ['System.AuthorizedDate'], [('oldValue', '2021-10-27T11:10:45.047Z')])
('change', ['System.RevisedDate', 'newValue'], ('2021-10-27T11:10:45.77Z', '9999-01-01T00:00:00Z'))
('add', ['System.RevisedDate'], [('oldValue', '2021-10-27T11:10:45.77Z')])
('change', ['System.ChangedDate', 'newValue'], ('2021-10-27T11:10:45.047Z', '2021-10-27T11:10:45.77Z'))
('add', ['System.ChangedDate'], [('oldValue', '2021-10-27T11:10:45.047Z')])
('change', ['System.Watermark', 'newValue'], (249, 250))
('add', ['System.Watermark'], [('oldValue', 249)])
('add', '', [('Microsoft.VSTS.Common.BacklogPriority', {'newValue': 599975506.0})])
有没有办法将其转换为字典或其他解析方式以便我可以获得 add
和 change
值?
预期输出
change
System.Rev
newValue
2
add
Microsoft.VSTS.Common.BacklogPriority
newValue': 599975506.0
或类似的东西,它可以以干净的格式显示添加和更改的值以发送到
db
列。
这就是我得到这个的方式
from dictdiffer import diff
for i, j in enumerate(rev_items):
try:
res = list(diff(rev_items[i], rev_items[i+1]))
for item in res:
print(item)
当我执行 dict(res)
时,它什么都不打印。
我试过的一种方法是这样的。
不使用列表理解
for item in res:
for c in range(len(item)):
print(item[c])
输出这个
change
['System.Rev', 'newValue']
(1, 2)
add
['System.Rev']
[('oldValue', 1)]
change
['System.AuthorizedDate', 'newValue']
('2021-10-27T11:10:45.047Z', '2021-10-27T11:10:45.77Z')
add
['System.AuthorizedDate']
[('oldValue', '2021-10-27T11:10:45.047Z')]
change
['System.RevisedDate', 'newValue']
('2021-10-27T11:10:45.77Z', '9999-01-01T00:00:00Z')
add
['System.RevisedDate']
[('oldValue', '2021-10-27T11:10:45.77Z')]
change
['System.ChangedDate', 'newValue']
('2021-10-27T11:10:45.047Z', '2021-10-27T11:10:45.77Z')
add
['System.ChangedDate']
[('oldValue', '2021-10-27T11:10:45.047Z')]
change
['System.Watermark', 'newValue']
(249, 250)
add
['System.Watermark']
[('oldValue', 249)]
add
[('Microsoft.VSTS.Common.BacklogPriority', {'newValue': 599975506.0})]
如何 get/parse 得到 add
和 change
值?
感谢 Barmar 的投入。我就是这样得到它的。
res = list(diff(rev_items[i], rev_items[i+1]))
for item in res:
for a, b in enumerate(item):
if item[a] == 'change' or item[a] == 'add':
print(item[a+1], ":", item[a+2])
输出
['System.Rev', 'newValue'] : (1, 2)
['System.Rev'] : [('oldValue', 1)]
['System.AuthorizedDate', 'newValue'] : ('2021-10-27T11:10:45.047Z', '2021-10-27T11:10:45.77Z')
['System.AuthorizedDate'] : [('oldValue', '2021-10-27T11:10:45.047Z')]
['System.RevisedDate', 'newValue'] : ('2021-10-27T11:10:45.77Z', '9999-01-01T00:00:00Z')
['System.RevisedDate'] : [('oldValue', '2021-10-27T11:10:45.77Z')]
['System.ChangedDate', 'newValue'] : ('2021-10-27T11:10:45.047Z', '2021-10-27T11:10:45.77Z')
['System.ChangedDate'] : [('oldValue', '2021-10-27T11:10:45.047Z')]
['System.Watermark', 'newValue'] : (249, 250)
['System.Watermark'] : [('oldValue', 249)]
: [('Microsoft.VSTS.Common.BacklogPriority', {'newValue': 599975506.0})]
我有一个 tuples
这样的列表。
('change', ['System.Rev', 'newValue'], (1, 2))
('add', ['System.Rev'], [('oldValue', 1)])
('change', ['System.AuthorizedDate', 'newValue'], ('2021-10-27T11:10:45.047Z', '2021-10-27T11:10:45.77Z'))
('add', ['System.AuthorizedDate'], [('oldValue', '2021-10-27T11:10:45.047Z')])
('change', ['System.RevisedDate', 'newValue'], ('2021-10-27T11:10:45.77Z', '9999-01-01T00:00:00Z'))
('add', ['System.RevisedDate'], [('oldValue', '2021-10-27T11:10:45.77Z')])
('change', ['System.ChangedDate', 'newValue'], ('2021-10-27T11:10:45.047Z', '2021-10-27T11:10:45.77Z'))
('add', ['System.ChangedDate'], [('oldValue', '2021-10-27T11:10:45.047Z')])
('change', ['System.Watermark', 'newValue'], (249, 250))
('add', ['System.Watermark'], [('oldValue', 249)])
('add', '', [('Microsoft.VSTS.Common.BacklogPriority', {'newValue': 599975506.0})])
有没有办法将其转换为字典或其他解析方式以便我可以获得 add
和 change
值?
预期输出
change
System.Rev
newValue
2
add
Microsoft.VSTS.Common.BacklogPriority
newValue': 599975506.0
或类似的东西,它可以以干净的格式显示添加和更改的值以发送到
db
列。
这就是我得到这个的方式
from dictdiffer import diff
for i, j in enumerate(rev_items):
try:
res = list(diff(rev_items[i], rev_items[i+1]))
for item in res:
print(item)
当我执行 dict(res)
时,它什么都不打印。
我试过的一种方法是这样的。 不使用列表理解
for item in res:
for c in range(len(item)):
print(item[c])
输出这个
change
['System.Rev', 'newValue']
(1, 2)
add
['System.Rev']
[('oldValue', 1)]
change
['System.AuthorizedDate', 'newValue']
('2021-10-27T11:10:45.047Z', '2021-10-27T11:10:45.77Z')
add
['System.AuthorizedDate']
[('oldValue', '2021-10-27T11:10:45.047Z')]
change
['System.RevisedDate', 'newValue']
('2021-10-27T11:10:45.77Z', '9999-01-01T00:00:00Z')
add
['System.RevisedDate']
[('oldValue', '2021-10-27T11:10:45.77Z')]
change
['System.ChangedDate', 'newValue']
('2021-10-27T11:10:45.047Z', '2021-10-27T11:10:45.77Z')
add
['System.ChangedDate']
[('oldValue', '2021-10-27T11:10:45.047Z')]
change
['System.Watermark', 'newValue']
(249, 250)
add
['System.Watermark']
[('oldValue', 249)]
add
[('Microsoft.VSTS.Common.BacklogPriority', {'newValue': 599975506.0})]
如何 get/parse 得到 add
和 change
值?
感谢 Barmar 的投入。我就是这样得到它的。
res = list(diff(rev_items[i], rev_items[i+1]))
for item in res:
for a, b in enumerate(item):
if item[a] == 'change' or item[a] == 'add':
print(item[a+1], ":", item[a+2])
输出
['System.Rev', 'newValue'] : (1, 2)
['System.Rev'] : [('oldValue', 1)]
['System.AuthorizedDate', 'newValue'] : ('2021-10-27T11:10:45.047Z', '2021-10-27T11:10:45.77Z')
['System.AuthorizedDate'] : [('oldValue', '2021-10-27T11:10:45.047Z')]
['System.RevisedDate', 'newValue'] : ('2021-10-27T11:10:45.77Z', '9999-01-01T00:00:00Z')
['System.RevisedDate'] : [('oldValue', '2021-10-27T11:10:45.77Z')]
['System.ChangedDate', 'newValue'] : ('2021-10-27T11:10:45.047Z', '2021-10-27T11:10:45.77Z')
['System.ChangedDate'] : [('oldValue', '2021-10-27T11:10:45.047Z')]
['System.Watermark', 'newValue'] : (249, 250)
['System.Watermark'] : [('oldValue', 249)]
: [('Microsoft.VSTS.Common.BacklogPriority', {'newValue': 599975506.0})]