data is a Python dictionary. It needs to be encoded as JSON before writing.
Use this for maximum compatibility (Python 2 and 3):
import json
with open('data.json', 'w') as f:
json.dump(data, f)
On a modern system (i.e. Python 3 and UTF-8 support), you can write a nicer file using:
import json
with open('data.json', 'w', encoding='utf-8') as f:
json.dump(data, f, ensure_ascii=False, indent=4)
See json documentation.
data is a Python dictionary. It needs to be encoded as JSON before writing.
Use this for maximum compatibility (Python 2 and 3):
import json
with open('data.json', 'w') as f:
json.dump(data, f)
On a modern system (i.e. Python 3 and UTF-8 support), you can write a nicer file using:
import json
with open('data.json', 'w', encoding='utf-8') as f:
json.dump(data, f, ensure_ascii=False, indent=4)
See json documentation.
To get utf8-encoded file as opposed to ascii-encoded in the accepted answer for Python 2 use:
import io, json
with io.open('data.txt', 'w', encoding='utf-8') as f:
f.write(json.dumps(data, ensure_ascii=False))
The code is simpler in Python 3:
import json
with open('data.txt', 'w') as f:
json.dump(data, f, ensure_ascii=False)
On Windows, the encoding='utf-8' argument to open is still necessary.
To avoid storing an encoded copy of the data in memory (result of dumps) and to output utf8-encoded bytestrings in both Python 2 and 3, use:
import json, codecs
with open('data.txt', 'wb') as f:
json.dump(data, codecs.getwriter('utf-8')(f), ensure_ascii=False)
The codecs.getwriter call is redundant in Python 3 but required for Python 2
Readability and size:
The use of ensure_ascii=False gives better readability and smaller size:
>>> json.dumps({'price': '€10'})
'{"price": "\\u20ac10"}'
>>> json.dumps({'price': '€10'}, ensure_ascii=False)
'{"price": "€10"}'
>>> len(json.dumps({'абвгд': 1}))
37
>>> len(json.dumps({'абвгд': 1}, ensure_ascii=False).encode('utf8'))
17
Further improve readability by adding flags indent=4, sort_keys=True (as suggested by dinos66) to arguments of dump or dumps. This way you'll get a nicely indented sorted structure in the json file at the cost of a slightly larger file size.
Storing list permanently and loading it in the exact same format
python - Writing list of objects to JSON file - Stack Overflow
python - Write a json file from list - Stack Overflow
How can I write data to a json file and also write the variable name?
I'm aiming to store a list of tickers permanently by writing it to a file that I can load when needed. Two things here:
-
It's not working because readlines() interprets the file as one element and not a list of separate elements. I don't see a way to specify the separator between elements as comma.
-
Making point 1 work should be very simple, but I'm surprised if code like what I have now (see below) is necessary. Seems so me that storing a list on disk and loading it in the exact same formatting should be a standard function. Is there something I'm overlooking?
My current code
Writing to file
file = open('TickerList.txt','w')
for item in items:
file.write(item)
file.close()Reading from file:
with open('TickerList.txt') as f:
ticker_list = f.readlines()You should use either dumps, or dump. Not both.
dump (my preference):
def create_json():
with open("./data/student.txt", "w") as file:
json.dump([ob.__dict__ for ob in stList], file)
OR dumps:
def create_json():
json_string = json.dumps([ob.__dict__ for ob in stList])
with open("./data/student.txt", "w") as file:
file.write(json_string)
As a side note, in the future you'll want to watch out for scoping as you're using the global object stList.
You are encoding your dictionary to JSON string twice. First you do this here:
json_string = json.dumps([ob.__dict__ for ob in stList])
and then once again, here:
json.dump(json_string, file)
Alter this line with file.write(json_string)
Just use json as usual:
import json
data = [{"nomineesWidgetModel":{"title":"","description":"", "refMarker":"ev_nom","eventEditionSummary":{"awards":[{"awardName":"Oscar","trivia":[]}]}}}]
with open('data.json', 'w') as f:
json.dump(data, f, indent=4)
Thanks to @Alexander explanation above, I was able to save the content I was scraping in a dict, and not a list, and then save as json while iterating the pages with:
with open('data.json', 'a') as file:
json.dump(data, file, indent=1)
Let's say I want to write two lists and a dict to a JSON file. I can do so with the following code:
import json
x = [1, 2, 3]
y = [4, 5, 6]
z = {"1": 1, "2": 2}
with open("test.json", "a", encoding="utf-8") as f:
json.dump(x, f)
json.dump(y, f)
json.dump(z, f)But what if I want the variable names written to the file as well? My goal is to be able to read / write / append to the two lists and the dict at a later time. To do so, I would need to access them by variable name. How can I achieve this?
Try this in one line using zip():
result = {'items':[{'time':i[0],'name':i[1], 'age':i[2], 'coins':i[3]} for i in zip(dates,names,number1,number2)]}
the result will be:
{'items': [
{'time': '01.03.2021 13:05:59', 'name': 'name1','age': 43,'coins': 3},
{'time': '01.03.2021 13:46:04', 'name': 'name2', 'age': 32, 'coins': 6},
{'time': '01.03.2021 14:05:59', 'name': 'name3', 'age': 12, 'coins': 6},
{'time': '01.03.2021 13:30:04', 'name': 'name2', 'age': 12, 'coins': 3}
]}
Simply create a list of dictionaries and use json for writing to file:
output = []
for name, num1, num2, date in zip(names, number1, number2, dates):
dic = dict()
dic["time"] = date
dic["name"] = name
dic["age"] = num1
dic["coins"] = num2
output.append(dic)
data = {"items": output}
import json
with open('out.json', 'w') as outfile:
json.dump(data, outfile)
Note: dic object could be removed but it looks readable that way.
Content of out.json:
{"items": [{"time": "01.03.2021 13:05:59", "name": "name1", "age": 43, "coins": 3}, {"time": "01.03.2021 13:46:04", "name": "name2", "age": 32, "coins": 6}, {"time": "01.03.2021 14:05:59", "name": "name3", "age": 12, "coins": 6}, {"time": "01.03.2021 13:30:04", "name": "name2", "age": 12, "coins": 3}]}