Python converting a text file into a json object

json, python

Solution

import json
data = dict(zip(comic_book_titles, dates))
json.dumps(data)

More info about `zip()`

Problem

I am not new to programming, but I am very new to python. I don't yet completely understand Python's data structures. Below is my problem. Given a txt file containing the text: FEB 5 ACTION COMICS #28 BATWING #28 CAPTAIN AMERICA #16 ... FEB 12 ABE SAPIEN #10 AMAZING SPIDER-MAN MOVIE ADAPT #2 BATMAN #28 ... I want to create a JSON object that looks like: {"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"} So far I have gotten to the point where I have two equal length lists that each contain the corresponding comic book titles and dates. For example, say I have the two lists below: ``` 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"] ``` How do I get the JSON object described above? Note, that I can't just enter in: ``` 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) ``` Because I am getting the data from a text file.

Original source