如何从字典列表中删除文本
How to remove text from list of dictionary
词典列表如下
[{'title': 'XXX#1092', 'views': '1,983,228 views'}, {'title': 'xxEP#1091', 'views': '2,559,581 views'}, {'title': 'xx', 'views': '183,545 views'}, {'title': ' Official Music Video', 'views': '439,534 views'}]
如何从 views
和 ,
中删除 views
键,预期输出
[{'title': 'XXX#1092', 'views': '1983228'}, {'title': 'xxEP#1091', 'views': '2559581'}, {'title': 'xx', 'views': '183545'}, {'title': ' Official Music Video', 'views': '439534'}]
d = [{'title': 'XXX#1092', 'views': '1,983,228 views'}, {'title': 'xxEP#1091', 'views': '2,559,581 views'}, {'title': 'xx', 'views': '183,545 views'}, {'title': ' Official Music Video', 'views': '439,534 views'}]
for k in d:
k["views"] = k["views"].replace(",","").replace("views","").strip()
print(d)
[{'title': 'XXX#1092', 'views': '1983228'},
{'title': 'xxEP#1091', 'views': '2559581'},
{'title': 'xx', 'views': '183545'},
{'title': ' Official Music Video', 'views': '439534'}]
videos = [
{'title': 'XXX#1092', 'views': '1,983,228 views'},
{'title': 'xxEP#1091', 'views': '2,559,581 views'},
{'title': 'xx', 'views': '183,545 views'},
{'title': ' Official Music Video', 'views': '439,534 views'}
]
for video in videos:
video["views"] = "".join(filter(str.isdigit, video["views"]))
print(videos)
输出:
[{'title': 'XXX#1092', 'views': '1983228'}, {'title': 'xxEP#1091', 'views': '2559581'}, {'title': 'xx', 'views': '183545'}, {'title': ' Official Music Video', 'views': '439534'}]
>>>
词典列表如下
[{'title': 'XXX#1092', 'views': '1,983,228 views'}, {'title': 'xxEP#1091', 'views': '2,559,581 views'}, {'title': 'xx', 'views': '183,545 views'}, {'title': ' Official Music Video', 'views': '439,534 views'}]
如何从 views
和 ,
中删除 views
键,预期输出
[{'title': 'XXX#1092', 'views': '1983228'}, {'title': 'xxEP#1091', 'views': '2559581'}, {'title': 'xx', 'views': '183545'}, {'title': ' Official Music Video', 'views': '439534'}]
d = [{'title': 'XXX#1092', 'views': '1,983,228 views'}, {'title': 'xxEP#1091', 'views': '2,559,581 views'}, {'title': 'xx', 'views': '183,545 views'}, {'title': ' Official Music Video', 'views': '439,534 views'}]
for k in d:
k["views"] = k["views"].replace(",","").replace("views","").strip()
print(d)
[{'title': 'XXX#1092', 'views': '1983228'},
{'title': 'xxEP#1091', 'views': '2559581'},
{'title': 'xx', 'views': '183545'},
{'title': ' Official Music Video', 'views': '439534'}]
videos = [
{'title': 'XXX#1092', 'views': '1,983,228 views'},
{'title': 'xxEP#1091', 'views': '2,559,581 views'},
{'title': 'xx', 'views': '183,545 views'},
{'title': ' Official Music Video', 'views': '439,534 views'}
]
for video in videos:
video["views"] = "".join(filter(str.isdigit, video["views"]))
print(videos)
输出:
[{'title': 'XXX#1092', 'views': '1983228'}, {'title': 'xxEP#1091', 'views': '2559581'}, {'title': 'xx', 'views': '183545'}, {'title': ' Official Music Video', 'views': '439534'}]
>>>