I have JSON data stored in the variable data
.
我将JSON数据存储在变量数据中。
I want to write this to a text file for testing so I don't have to grab the data from the server each time.
我想将它写入一个文本文件进行测试,这样我就不必每次都从服务器获取数据。
Currently, I am trying this:
目前,我正在尝试:
obj = open('data.txt', 'wb')
obj.write(data)
obj.close
And am receiving the error:
并且正在接收错误:
TypeError: must be string or buffer, not dict
类型错误:必须是字符串或缓冲区,而不是dict类型
How to fix this?
如何解决这个问题?
10 个解决方案
#1
1238
You forgot the actual JSON part - data
is a dictionary and not yet JSON-encoded. Write it like this:
您忘记了实际的JSON部分——数据是一个字典,还没有JSON编码。这样写:
import json
with open('data.txt', 'w') as outfile:
json.dump(data, outfile)
Note: Works on both 3.x and 2.x .
注意:两者都适用。x和2。x。
#2
191
To get utf8-encoded file as opposed to ascii-encoded in the accepted answer for Python 2 use:
要获得utf8编码的文件,而不是Python 2的公认答案中使用的ascii编码:
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:
在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.
在Windows上,编码='utf-8'参数打开仍然是必要的。
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:
为了避免在内存中存储已编码的数据副本(转储的结果),并在Python 2和3中输出utf8编码的bytestring,请使用:
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
编解码器。getwriter调用在Python 3中是多余的,但在Python 2中是必需的
Readability and size:
可读性和尺寸:
The use of ensure_acsii=False
gives better readability and smaller size:
ensure_acsii=False的使用使其具有更好的可读性和更小的大小:
>>> 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.
通过向转储或转储的参数添加标志缩进=4、sort_keys=True(如dinos66所建议的),进一步提高了可读性。通过这种方式,您可以在json文件中以稍微大一点的文件大小来得到一个良好的缩进排序结构。
#3
126
I would answer with slight modification with aforementioned answers and that is to write a prettified JSON file which human eyes can read better. For this, pass sort_keys
as True
and indent
with 4 space characters and you are good to go. Also take care of ensuring that the ascii codes will not be written in your JSON file:
我将对前面提到的答案进行一些修改,即编写一个经过修饰的JSON文件,使人眼能够更好地阅读。为此,将sort_keys传递为True并使用4个空格字符进行缩进,这样就可以了。还要确保ascii码不会写入JSON文件:
with open('data.txt', 'w') as outfile:
json.dump(jsonData, outfile, sort_keys = True, indent = 4,
ensure_ascii = False)
#4
75
Read and write JSON files with Python 2+3; works with unicode
# -*- coding: utf-8 -*-
import json
# Make it work for Python 2+3 and with Unicode
import io
try:
to_unicode = unicode
except NameError:
to_unicode = str
# Define data
data = {'a list': [1, 42, 3.141, 1337, 'help', u'€'],
'a string': 'bla',
'another dict': {'foo': 'bar',
'key': 'value',
'the answer': 42}}
# Write JSON file
with io.open('data.json', 'w', encoding='utf8') as outfile:
str_ = json.dumps(data,
indent=4, sort_keys=True,
separators=(',', ': '), ensure_ascii=False)
outfile.write(to_unicode(str_))
# Read JSON file
with open('data.json') as data_file:
data_loaded = json.load(data_file)
print(data == data_loaded)
Explanation of the parameters of json.dump
:
json.dump参数说明:
-
indent
: Use 4 spaces to indent each entry, e.g. when a new dict is started (otherwise all will be in one line), - 缩进:使用4个空格对每个条目进行缩进,例如,当一个新命令启动时(否则所有条目都将在一行中),
-
sort_keys
: sort the keys of dictionaries. This is useful if you want to compare json files with a diff tool / put them under version control. - sort_keys:对字典的键进行排序。如果您希望将json文件与diff工具进行比较,或者将它们置于版本控制之下,这将非常有用。
-
separators
: To prevent Python from adding trailing whitespaces - 分隔符:防止Python添加拖尾白空间
With a package
Have a look at my utility package mpu
for a super simple and easy to remember one:
看看我的实用软件包mpu超级简单和容易记住一个:
import mpu.io
data = mpu.io.read('example.json')
mpu.io.write('example.json', data)
Created JSON file
{
"a list":[
1,
42,
3.141,
1337,
"help",
"€"
],
"a string":"bla",
"another dict":{
"foo":"bar",
"key":"value",
"the answer":42
}
}
Common file endings
.json
. json
Alternatives
- CSV: Super simple format (read & write)
- CSV:超简单格式(读写)
- JSON: Nice for writing human-readable data; VERY commonly used (read & write)
- JSON:适合编写人类可读数据;非常常用(读写)
- YAML: YAML is a superset of JSON, but easier to read (read & write, comparison of JSON and YAML)
- YAML: YAML是JSON的超集,但是更容易读(读和写,JSON和YAML的比较)
- pickle: A Python serialization format (read & write)
- pickle: Python的序列化格式(读和写)
- MessagePack (Python package): More compact representation (read & write)
- MessagePack (Python包):更紧凑的表示(读和写)
- HDF5 (Python package): Nice for matrices (read & write)
- HDF5 (Python包):适用于矩阵(读写)
- XML: exists too *sigh* (read & write)
- XML:存在太*叹气*(读和写)
For your application, the following might be important:
对于您的申请,以下内容可能很重要:
- Support by other programming languages
- 支持其他编程语言
- Reading / writing performance
- 读/写性能
- Compactness (file size)
- 密实度(文件大小)
See also: Comparison of data serialization formats
参见:数据序列化格式的比较
In case you are rather looking for a way to make configuration files, you might want to read my short article Configuration files in Python
如果您正在寻找一种生成配置文件的方法,您可能想要在Python中阅读我的文章配置文件
#5
16
For those of you who are trying to dump greek or other "exotic" languages such as me but are also having problems (unicode errors) with weird characters such as the peace symbol (\u262E) or others which are often contained in json formated data such as Twitter's, the solution could be as follows (sort_keys is obviously optional):
对于那些试图抛售希腊或其他“异国情调”语言如我,但也有问题(unicode错误)与和平象征等奇怪的字符(\ u262E)或其他通常包含在json格式化数据如Twitter的,解决方案可能是如下(sort_keys显然是可选的):
import codecs, json
with codecs.open('data.json', 'w', 'utf8') as f:
f.write(json.dumps(data, sort_keys = True, ensure_ascii=False))
#6
9
I don't have enough reputation to add in comments, so I just write some of my findings of this annoying TypeError here:
我没有足够的声誉来添加评论,所以我只写了一些关于这个烦人的类型错误的发现:
Basically, I think it's a bug in the json.dump()
function in Python 2 only - It can't dump a Python (dictionary / list) data containing non-ASCII characters, even you open the file with the encoding = 'utf-8'
parameter. (i.e. No matter what you do). But, json.dumps()
works on both Python 2 and 3.
基本上,我认为这只是Python 2中json.dump()函数中的一个bug—它不能转储包含非ascii字符的Python(字典/列表)数据,即使您使用编码= 'utf-8'参数打开文件。(也就是说,不管你做什么)。但是,json.dumps()同时适用于Python 2和3。
To illustrate this, following up phihag's answer: the code in his answer breaks in Python 2 with exception TypeError: must be unicode, not str
, if data
contains non-ASCII characters. (Python 2.7.6, Debian):
为了说明这一点,按照phihag的答案:如果数据包含非ascii字符,那么他的答案中的代码将在Python 2中出现异常类型错误:必须是unicode,而不是str。(Python 2.7.6,Debian):
import json
data = {u'\u0430\u0431\u0432\u0433\u0434': 1} #{u'абвгд': 1}
with open('data.txt', 'w') as outfile:
json.dump(data, outfile)
It however works fine in Python 3.
但是它在Python 3中工作得很好。
#7
4
Write a data in file using JSON use json.dump() or json.dumps() used. write like this to store data in file.
使用JSON将数据写入文件,使用JSON .dump()或使用JSON .dumps()。像这样编写以将数据存储在文件中。
import json
data = [1,2,3,4,5]
with open('no.txt', 'w') as txtfile:
json.dump(data, txtfile)
this example in list is store to a file.
列表中的这个例子是存储到一个文件。
#8
3
json.dump(data, open('data.txt', 'wb'))
#9
2
if you are trying to write a pandas dataframe into a file using a json format i'd recommend this
如果您试图使用json格式将熊猫数据aframe写入文件,我建议您这样做
destination='filepath'
saveFile = open(destination, 'w')
saveFile.write(df.to_json())
saveFile.close()
#10
0
Here is a useful structure to both read and write a file in Python 3.
下面是在Python 3中读写文件的有用结构。
from json import dump, load
from time import sleep
from random import random
def json_file(path, data = None, delay = 0.1):
while True:
try:
if data == None:
with open(path, "r", encoding = "utf-8") as f:
return load(f)
else:
with open(path, "w", encoding = "utf-8") as f:
return dump(data, f)
except:
sleep(random()*delay) # concurrency
#1
1238
You forgot the actual JSON part - data
is a dictionary and not yet JSON-encoded. Write it like this:
您忘记了实际的JSON部分——数据是一个字典,还没有JSON编码。这样写:
import json
with open('data.txt', 'w') as outfile:
json.dump(data, outfile)
Note: Works on both 3.x and 2.x .
注意:两者都适用。x和2。x。
#2
191
To get utf8-encoded file as opposed to ascii-encoded in the accepted answer for Python 2 use:
要获得utf8编码的文件,而不是Python 2的公认答案中使用的ascii编码:
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:
在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.
在Windows上,编码='utf-8'参数打开仍然是必要的。
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:
为了避免在内存中存储已编码的数据副本(转储的结果),并在Python 2和3中输出utf8编码的bytestring,请使用:
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
编解码器。getwriter调用在Python 3中是多余的,但在Python 2中是必需的
Readability and size:
可读性和尺寸:
The use of ensure_acsii=False
gives better readability and smaller size:
ensure_acsii=False的使用使其具有更好的可读性和更小的大小:
>>> 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.
通过向转储或转储的参数添加标志缩进=4、sort_keys=True(如dinos66所建议的),进一步提高了可读性。通过这种方式,您可以在json文件中以稍微大一点的文件大小来得到一个良好的缩进排序结构。
#3
126
I would answer with slight modification with aforementioned answers and that is to write a prettified JSON file which human eyes can read better. For this, pass sort_keys
as True
and indent
with 4 space characters and you are good to go. Also take care of ensuring that the ascii codes will not be written in your JSON file:
我将对前面提到的答案进行一些修改,即编写一个经过修饰的JSON文件,使人眼能够更好地阅读。为此,将sort_keys传递为True并使用4个空格字符进行缩进,这样就可以了。还要确保ascii码不会写入JSON文件:
with open('data.txt', 'w') as outfile:
json.dump(jsonData, outfile, sort_keys = True, indent = 4,
ensure_ascii = False)
#4
75
Read and write JSON files with Python 2+3; works with unicode
# -*- coding: utf-8 -*-
import json
# Make it work for Python 2+3 and with Unicode
import io
try:
to_unicode = unicode
except NameError:
to_unicode = str
# Define data
data = {'a list': [1, 42, 3.141, 1337, 'help', u'€'],
'a string': 'bla',
'another dict': {'foo': 'bar',
'key': 'value',
'the answer': 42}}
# Write JSON file
with io.open('data.json', 'w', encoding='utf8') as outfile:
str_ = json.dumps(data,
indent=4, sort_keys=True,
separators=(',', ': '), ensure_ascii=False)
outfile.write(to_unicode(str_))
# Read JSON file
with open('data.json') as data_file:
data_loaded = json.load(data_file)
print(data == data_loaded)
Explanation of the parameters of json.dump
:
json.dump参数说明:
-
indent
: Use 4 spaces to indent each entry, e.g. when a new dict is started (otherwise all will be in one line), - 缩进:使用4个空格对每个条目进行缩进,例如,当一个新命令启动时(否则所有条目都将在一行中),
-
sort_keys
: sort the keys of dictionaries. This is useful if you want to compare json files with a diff tool / put them under version control. - sort_keys:对字典的键进行排序。如果您希望将json文件与diff工具进行比较,或者将它们置于版本控制之下,这将非常有用。
-
separators
: To prevent Python from adding trailing whitespaces - 分隔符:防止Python添加拖尾白空间
With a package
Have a look at my utility package mpu
for a super simple and easy to remember one:
看看我的实用软件包mpu超级简单和容易记住一个:
import mpu.io
data = mpu.io.read('example.json')
mpu.io.write('example.json', data)
Created JSON file
{
"a list":[
1,
42,
3.141,
1337,
"help",
"€"
],
"a string":"bla",
"another dict":{
"foo":"bar",
"key":"value",
"the answer":42
}
}
Common file endings
.json
. json
Alternatives
- CSV: Super simple format (read & write)
- CSV:超简单格式(读写)
- JSON: Nice for writing human-readable data; VERY commonly used (read & write)
- JSON:适合编写人类可读数据;非常常用(读写)
- YAML: YAML is a superset of JSON, but easier to read (read & write, comparison of JSON and YAML)
- YAML: YAML是JSON的超集,但是更容易读(读和写,JSON和YAML的比较)
- pickle: A Python serialization format (read & write)
- pickle: Python的序列化格式(读和写)
- MessagePack (Python package): More compact representation (read & write)
- MessagePack (Python包):更紧凑的表示(读和写)
- HDF5 (Python package): Nice for matrices (read & write)
- HDF5 (Python包):适用于矩阵(读写)
- XML: exists too *sigh* (read & write)
- XML:存在太*叹气*(读和写)
For your application, the following might be important:
对于您的申请,以下内容可能很重要:
- Support by other programming languages
- 支持其他编程语言
- Reading / writing performance
- 读/写性能
- Compactness (file size)
- 密实度(文件大小)
See also: Comparison of data serialization formats
参见:数据序列化格式的比较
In case you are rather looking for a way to make configuration files, you might want to read my short article Configuration files in Python
如果您正在寻找一种生成配置文件的方法,您可能想要在Python中阅读我的文章配置文件
#5
16
For those of you who are trying to dump greek or other "exotic" languages such as me but are also having problems (unicode errors) with weird characters such as the peace symbol (\u262E) or others which are often contained in json formated data such as Twitter's, the solution could be as follows (sort_keys is obviously optional):
对于那些试图抛售希腊或其他“异国情调”语言如我,但也有问题(unicode错误)与和平象征等奇怪的字符(\ u262E)或其他通常包含在json格式化数据如Twitter的,解决方案可能是如下(sort_keys显然是可选的):
import codecs, json
with codecs.open('data.json', 'w', 'utf8') as f:
f.write(json.dumps(data, sort_keys = True, ensure_ascii=False))
#6
9
I don't have enough reputation to add in comments, so I just write some of my findings of this annoying TypeError here:
我没有足够的声誉来添加评论,所以我只写了一些关于这个烦人的类型错误的发现:
Basically, I think it's a bug in the json.dump()
function in Python 2 only - It can't dump a Python (dictionary / list) data containing non-ASCII characters, even you open the file with the encoding = 'utf-8'
parameter. (i.e. No matter what you do). But, json.dumps()
works on both Python 2 and 3.
基本上,我认为这只是Python 2中json.dump()函数中的一个bug—它不能转储包含非ascii字符的Python(字典/列表)数据,即使您使用编码= 'utf-8'参数打开文件。(也就是说,不管你做什么)。但是,json.dumps()同时适用于Python 2和3。
To illustrate this, following up phihag's answer: the code in his answer breaks in Python 2 with exception TypeError: must be unicode, not str
, if data
contains non-ASCII characters. (Python 2.7.6, Debian):
为了说明这一点,按照phihag的答案:如果数据包含非ascii字符,那么他的答案中的代码将在Python 2中出现异常类型错误:必须是unicode,而不是str。(Python 2.7.6,Debian):
import json
data = {u'\u0430\u0431\u0432\u0433\u0434': 1} #{u'абвгд': 1}
with open('data.txt', 'w') as outfile:
json.dump(data, outfile)
It however works fine in Python 3.
但是它在Python 3中工作得很好。
#7
4
Write a data in file using JSON use json.dump() or json.dumps() used. write like this to store data in file.
使用JSON将数据写入文件,使用JSON .dump()或使用JSON .dumps()。像这样编写以将数据存储在文件中。
import json
data = [1,2,3,4,5]
with open('no.txt', 'w') as txtfile:
json.dump(data, txtfile)
this example in list is store to a file.
列表中的这个例子是存储到一个文件。
#8
3
json.dump(data, open('data.txt', 'wb'))
#9
2
if you are trying to write a pandas dataframe into a file using a json format i'd recommend this
如果您试图使用json格式将熊猫数据aframe写入文件,我建议您这样做
destination='filepath'
saveFile = open(destination, 'w')
saveFile.write(df.to_json())
saveFile.close()
#10
0
Here is a useful structure to both read and write a file in Python 3.
下面是在Python 3中读写文件的有用结构。
from json import dump, load
from time import sleep
from random import random
def json_file(path, data = None, delay = 0.1):
while True:
try:
if data == None:
with open(path, "r", encoding = "utf-8") as f:
return load(f)
else:
with open(path, "w", encoding = "utf-8") as f:
return dump(data, f)
except:
sleep(random()*delay) # concurrency