我不是编程新手,但我是python的新手。我还没有完全理解Python的数据结构。以下是我的问题。
给定包含文本的txt文件:
FEB 5
动作漫画#28
BATWING#28
CAPTAIN AMERICA#16
...
FEB 12
ABE SAPIEN#10
惊人的蜘蛛侠电影适应#2
BATMAN#28
...
我想创建一个看起来像这样的JSON对象:
{"行动动作#28":" FEB 5"," BATWING#28":" FEB 5",&#34 ; CAPTAIN AMERICA#16":" FEB 5"," ABE SAPIEN#10":" FEB 12","令人惊叹的蜘蛛侠电影ADAPT#2":" FEB 12"," BATMAN#28":" FEB 12"}
到目前为止,我已经达到了两个相等长度的列表,每个列表都包含相应的漫画书标题和日期。例如,假设我有以下两个列表:
comic_book_titles = ["ACTION COMICS #28", "BATWING #28", "CAPTAIN AMERICA #16", "ABE SAPIEN #10", "AMAZING SPIDER-MAN MOVIE ADAPT #2", "BATMAN #28"]
dates = ["FEB 5", "FEB 5", "FEB 5", "FEB 12", "FEB 12", "FEB 12"]
如何获取上述JSON对象?请注意,我无法进入:
import json
data = [{"ACTION COMIC #28":"FEB 5", "BATWING #28":"FEB 5", "CAPTAIN AMERICA #16":"FEB 5", "ABE SAPIEN #10":"FEB 12", "AMAZING SPIDER-MAN MOVIE ADAPT #2":"FEB 12", "BATMAN #28":"FEB 12"}]
json.dump(data)
因为我从文本文件中获取数据。
答案 0 :(得分:6)
import json
data = dict(zip(comic_book_titles, dates))
json.dumps(data)