I have a 2GB file titled 'Apps' that I need to import. The file is composed of nested dictionaries (snippet below). How can I import this in a way that packages the file into a dictionary variable? I realize I could open the file and simply assign, but at 2GB each, I don't want to have to open each file and make the assignment. Thanks
{
'responseHeader':{
'status':0,
'QTime':35,
'params':{
'sort':'appTitle asc',
'indent':'true',
'start':'400',
'q':'*:*',
'wt':'python',
'fq':['status:"A"',
'-developerWebsite:["" TO *]',
'intNumDownloads:[0 TO *]'],
'rows':'450'}},
'response':{'numFound':771005,'start':400,'docs':[]}}
If your file contains a well-formed Python dict, you can load it in one line like this:
with open('dictfile') as f: my_dict = eval(f.read())
The syntax
with open(filename) as f:
# do something
is short for
f = open(filename)
# do something
f.close()
It's a good practice to get into, since it makes sure that your stream is closed at the end. You can use this for database connections, http connections etc as well.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.