Python write json to file
I know the traditional way is to dump a dictionary list using json. Is there anyway to stream it into a json file, i. I know this is a year late, but the issue is still open and I'm surprised the json. The potential problem with iterencode in this example, is that you would want to have an iterative handle on the large data set by using a generator, and json encode does not serialize generators.
The usage from here on is quite simple. Get the generator handle, pass it into the StreamArray class, pass the stream array object into iterencode and iterate over the chunks. The chunks will be json formated output which can be directly written to file. Sadly the json library does not have any incremental writing facilities, and therefore cannot do what you want. That's clearly going to be a very large file - would some other representation be more appropriate?
Otherwise the best suggestion I can make is to dump each list entry to an in-memory structure and write them out with the necessary delimiters [ at the beginning, ],[ between entries and ] at the end to try and construct the JSON that you need. If formatting is important you should know that the wrapper test your program writes will destroy correct indentation, but indentation is only for humans so it shouldn't make any difference to the semantics of the JSON structure.
You could also assume you have an iterable it that you want to write out to a file handle fh as a large JSON array of records, and do the following, which I think is the most straightforward approach:. How are we doing? Please help us improve Stack Overflow.
Print the Fibonacci sequence. Check leap year. Reference Materials Built-in Functions. List Methods. Dictionary Methods. String Methods. Start Learning Python. Explore Python Examples. Python List extend. Suppose, you have a file named person. We're your Python code review copilot. Get code improvements right in your pull request with our GitHub code review bot.
Configuring this check Code Review Doctor will run this check by default. Read more about configuring Code Review Doctor. Yet, if you do not mind having just a string of an array, this makes the dictionary more readable. Then exchange:. How are we doing? Please help us improve Stack Overflow.
Take our short survey. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Ask Question. Asked 9 years, 4 months ago. Active 3 months ago. Viewed 1. I have JSON data stored in the variable data. For flags when opening file: Here, we used "w" letter in our argument, which indicates write and will create a file if it does not exist in library Plus sign indicates both read and write, guru Add a comment.
Active Oldest Votes. Write it like this for maximum compatibility Python 2 and 3 : import json with open 'data. Python 3 and UTF-8 support , you can write a nicer file with import json with open 'data. Do you mean json. TerminalDilettante json. See: this answer — klaas. On Python 3, json. You'd get a TypeError if the file was opened with wb.
On older Python versions, both w nand wb work. An explicit encoding is not necessary since the output of json. Show 22 more comments. To get utf8 -encoded file as opposed to ascii -encoded in the accepted answer for Python 2 use: import io, json with io. 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.
Antony Hatchkins Antony Hatchkins The unicode is superfluous - the result of json. Note that this fails in 3.
0コメント