python the json object must be str,not bytes'什么问题 Python中object has no attribute...

python TypeError: must be str, not NoneType\u600e\u4e48\u89e3\u51b3

\u51fa\u73b0\u8fd9\u79cd\u60c5\u51b5\u4e00\u822c\u90fd\u662f\u83b7\u53d6\u5185\u5bb9\u65f6\u5f97\u5230\u7684\u662fjson\u683c\u5f0f\u6570\u636e\u6216\u8005\u8bf4\u662f\u5b57\u5178\u683c\u5f0f\u6570\u636e\uff0c\u7136\u540e\u628ajson\u683c\u5f0f\u6570\u636e\u5199\u5165\u6587\u4ef6\u65f6\u624d\u4f1a\u62a5\u9519\u3002\u89e3\u51b3\u65b9\u6cd5\u5982\u4e0b\uff1a
1\u3001\u9996\u5148\u5728Python\u4e2d\u53ef\u4ee5\u8c03\u7528json\u6a21\u5757\u4ee5\u5904\u7406json\u683c\u5f0f\u6570\u636e\uff0c\u8fd9\u6837json\u683c\u5f0f\u6570\u636e\u4fbf\u53ef\u4ee5\u8f6c\u6362\u6210\u5b57\u7b26\u4e32\u4e86\u3002

2\u3001\u5982\u56fe\uff0c\u8f6c\u6362\u65b9\u6cd5\u5c31\u662fdumps()\uff0c\u7136\u540e\u628a\u8f6c\u6362\u540e\u7684\u5b57\u7b26\u4e32\u8d4b\u503c\u7ed9aa\u5373\u53ef\u3002

3\u3001\u8fd9\u6837\u6267\u884c\u7684\u65f6\u5019\u5c31\u4e0d\u4f1a\u62a5\u9519\u4e86\uff0c\u4f46\u662f\u8fd9\u6837\u76f4\u63a5\u5199\u5165\u6587\u4ef6\u4f1a\u51fa\u73b0\u\u4e71\u7801\uff0c\u8fd9\u5c31\u6d89\u53ca\u5230\u7f16\u7801\u95ee\u9898\u4e86\u3002

4\u3001\u6240\u4ee5\u8981\u5728dumps\u91cc\u9762\u6dfb\u52a0\u4e00\u4e2a\u53c2\u6570\uff0censure_ascii\uff0c\u5e76\u4e14\u8bbe\u7f6e\u4e3aFALSE\u9009\u9879\u624d\u53ef\u4ee5\u3002

5\u3001\u6700\u540e\u518d\u6b21\u8fd0\u884c\u5373\u53ef\u628a\u5b57\u5178\u7684\u5185\u5bb9\u6210\u529f\u7684\u5199\u5165\u5230\u6587\u4ef6\u5f53\u4e2d\u53bb\u4e86\u3002

\u6ca1\u6709\u7ee7\u627fObject\u3002
\u9996\u5148\u4ec0\u4e48\u662f\u65b0\u5f0f\u7c7b \u7ecf\u5178\u7c7b\u5462\uff1a
#\u65b0\u5f0f\u7c7b\u662f\u6307\u7ee7\u627fobject\u7684\u7c7b
class A(obect):
#\u7ecf\u5178\u7c7b\u662f\u6307\u6ca1\u6709\u7ee7\u627fobject\u7684\u7c7b
class A:
Python\u4e2d\u63a8\u8350\u5927\u5bb6\u4f7f\u7528\u65b0\u5f0f\u7c7b 1.\u65b0\u7684\u80af\u5b9a\u597d\u54c8\uff0c\u5df2\u7ecf\u517c\u5bb9\u7ecf\u5178\u7c7b
2.\u4fee\u590d\u4e86\u7ecf\u5178\u7c7b\u4e2d\u591a\u7ee7\u627f\u51fa\u73b0\u7684bug
\u4e0b\u9762\u6211\u4eec\u7740\u91cd\u8bf4\u4e00\u4e0b\u591a\u7ee7\u627f\u7684bug \u5982\u56fe\uff1a
BC \u4e3aA\u7684\u5b50\u7c7b\uff0c D\u4e3aBC\u7684\u5b50\u7c7b \uff0cA\u4e2d\u6709save\u65b9\u6cd5\uff0cC\u5bf9\u5176\u8fdb\u884c\u4e86\u91cd\u5199
\u5728\u7ecf\u5178\u7c7b\u4e2d \u8c03\u7528D\u7684save\u65b9\u6cd5 \u641c\u7d22\u6309\u6df1\u5ea6\u4f18\u5148 \u8def\u5f84B-A-C\uff0c \u6267\u884c\u7684\u4e3aA\u4e2dsave \u663e\u7136\u4e0d\u5408\u7406
\u5728\u65b0\u5f0f\u7c7b\u7684 \u8c03\u7528D\u7684save\u65b9\u6cd5 \u641c\u7d22\u6309\u5e7f\u5ea6\u4f18\u5148 \u8def\u5f84B-C-A\uff0c \u6267\u884c\u7684\u4e3aC\u4e2dsave
#\u7ecf\u5178\u7c7b
class A:
def __init__(self):
print 'this is A'
def save(self):
print 'come from A'
class B(A):
def __init__(self):
print 'this is B'
class C(A):
def __init__(self):
print 'this is C'
def save(self):
print 'come from C'
class D(B,C):
def __init__(self):
print 'this is D'
d1=D()
d1.save() #\u7ed3\u679c\u4e3a'come from A
#\u65b0\u5f0f\u7c7b
class A(object):
def __init__(self):
print 'this is A'
def save(self):
print 'come from A'
class B(A):
def __init__(self):
print 'this is B'
class C(A):
def __init__(self):
print 'this is C'
def save(self):
print 'come from C'
class D(B,C):
def __init__(self):
print 'this is D'
d1=D()
d1.save() #\u7ed3\u679c\u4e3a'come from C'
Python
\uff08\u82f1\u56fd\u53d1\u97f3\uff1a/ˈpaɪ\u03b8ən/ \u7f8e\u56fd\u53d1\u97f3\uff1a/ˈpaɪ\u03b8\u0251ːn/\uff09, \u662f\u4e00\u79cd\u9762\u5411\u5bf9\u8c61\u7684\u89e3\u91ca\u578b\u8ba1\u7b97\u673a\u7a0b\u5e8f\u8bbe\u8ba1\u8bed\u8a00\uff0c\u7531\u8377\u5170\u4ebaGuido van Rossum\u4e8e1989\u5e74\u53d1\u660e\uff0c\u7b2c\u4e00\u4e2a\u516c\u5f00\u53d1\u884c\u7248\u53d1\u884c\u4e8e1991\u5e74\u3002
Python\u662f\u7eaf\u7cb9\u7684\u81ea\u7531\u8f6f\u4ef6\uff0c \u6e90\u4ee3\u7801\u548c\u89e3\u91ca\u5668CPython\u9075\u5faa GPL(GNU General Public License)\u534f\u8bae\u3002Python\u8bed\u6cd5\u7b80\u6d01\u6e05\u6670\uff0c\u7279\u8272\u4e4b\u4e00\u662f\u5f3a\u5236\u7528\u7a7a\u767d\u7b26(white space)\u4f5c\u4e3a\u8bed\u53e5\u7f29\u8fdb\u3002
Python\u5177\u6709\u4e30\u5bcc\u548c\u5f3a\u5927\u7684\u5e93\u3002\u5b83\u5e38\u88ab\u6635\u79f0\u4e3a\u80f6\u6c34\u8bed\u8a00\uff0c\u80fd\u591f\u628a\u7528\u5176\u4ed6\u8bed\u8a00\u5236\u4f5c\u7684\u5404\u79cd\u6a21\u5757\uff08\u5c24\u5176\u662fC/C++\uff09\u5f88\u8f7b\u677e\u5730\u8054\u7ed3\u5728\u4e00\u8d77\u3002\u5e38\u89c1\u7684\u4e00\u79cd\u5e94\u7528\u60c5\u5f62\u662f\uff0c\u4f7f\u7528Python\u5feb\u901f\u751f\u6210\u7a0b\u5e8f\u7684\u539f\u578b\uff08\u6709\u65f6\u751a\u81f3\u662f\u7a0b\u5e8f\u7684\u6700\u7ec8\u754c\u9762\uff09\uff0c\u7136\u540e\u5bf9\u5176\u4e2d\u6709\u7279\u522b\u8981\u6c42\u7684\u90e8\u5206\uff0c\u7528\u66f4\u5408\u9002\u7684\u8bed\u8a00\u6539\u5199\uff0c\u6bd4\u59823D\u6e38\u620f\u4e2d\u7684\u56fe\u5f62\u6e32\u67d3\u6a21\u5757\uff0c\u6027\u80fd\u8981\u6c42\u7279\u522b\u9ad8\uff0c\u5c31\u53ef\u4ee5\u7528C/C++\u91cd\u5199\uff0c\u800c\u540e\u5c01\u88c5\u4e3aPython\u53ef\u4ee5\u8c03\u7528\u7684\u6269\u5c55\u7c7b\u5e93\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\u5728\u60a8\u4f7f\u7528\u6269\u5c55\u7c7b\u5e93\u65f6\u53ef\u80fd\u9700\u8981\u8003\u8651\u5e73\u53f0\u95ee\u9898\uff0c\u67d0\u4e9b\u53ef\u80fd\u4e0d\u63d0\u4f9b\u8de8\u5e73\u53f0\u7684\u5b9e\u73b0\u3002
7\u670820\u65e5\uff0cIEEE\u53d1\u5e032017\u5e74\u7f16\u7a0b\u8bed\u8a00\u6392\u884c\u699c\uff1aPython\u9ad8\u5c45\u9996\u4f4d\u3002

翻译过来就是json对象必须是字符串类型,不是字节类型
你需要转换bytes类型
有如下bytes对象
a= b'{"a":"bcde"}'
你需要如下转换方式
a.decode("utf-8")
最后解析:json.loads(a)
需要说明的是该bytes对象必须是可解析否则会报ValueError异常

扩展阅读:黑马java视频 ... python for beginners ... python手机版下载安装 ... python run module ... 在线可视化python网站 ... python raise ... python网站入口 ... the pastwithin蝴蝶 ... the python challenge ...

本站交流只代表网友个人观点,与本站立场无关
欢迎反馈与建议,请联系电邮
2024© 车视网