`
SunnyYoona
  • 浏览: 369812 次
社区版块
存档分类
最新评论

[Python]json对象转换出错expected string or buffer python

 
阅读更多

【问题】

今天在使用python中的json转换碰到一个问题:


【代码】

comments.json

{
	"count":"2",
	"page":"1",
	"comments":[
		{
			"content":"helloworld",
			"user":{
				"id":"0001",
				"name":"xiaosi"
			},
			"source":{
				"link":"http://mobile.youku.co",
				"name":"iPhone"
			}
		},
		{
			"content":"welcome to china",
			"user":{
				"id":"0002",
				"name":"sjf"
			},
			"source":{
				"link":"http://mobile.youku.co",
				"name":"android"
			}
		}
	]
}
Test.py
# coding=utf-8

import json
file = file("D:\\项目\python\comments.json")
data = json.loads(file)  


【分析解决】

经过调试,最终发现,python中默认使用单引号表示字符串"'"所以当,使用字符串符值以后,python会把双引号转换为单引号。

举例:

s = {
	"count":"2",
	"page":"1",
	"comments":[
		{
			"content":"helloworld",
			"user":{
				"id":"0001",
				"name":"xiaosi"
			},
			"source":{
				"link":"http://mobile.youku.co",
				"name":"iPhone"
			}
		},
		{
			"content":"welcome to china",
			"user":{
				"id":"0002",
				"name":"sjf"
			},
			"source":{
				"link":"http://mobile.youku.co",
				"name":"android"
			}
		}
	]
}

print s



而json是不支持单引号的。可以用下面的方法转换

json_string=json.dumps(s)

str=json.loads(json_string)








分享到:
评论

相关推荐

    python 使用Flask 出现 ValueError: Expected object or value 问题解决

    我用pandas来读取json文件 调试是正常的。 服务跑起来是出现这样了这样的错误。 首先我的manager.文件与我处理数据的py文件不在同一级目录。 文件结构大致如下: project clean_data data.py test.json manager.py...

    Python Numpy:找到list中的np.nan值方法

    今天小编就为大家分享一篇Python Numpy:找到list中的np.nan值方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

    python pyinstaller3.4 win10补丁,修复环境变量错误

    Python3.7 在使用pyinstaller3.4打包发布可执行文件时报错:TypeError: expected str, bytes or os.PathLike object, not NoneType pyinstall。 下载后替换项目根目录下\venv\Lib\site-packages\PyInstaller\depend\...

    cpp-JSONC的JSON解析器

    JSON : C 的JSON 解析器

    Python & XML - XML Processing with Python, First Edition (2002).pdf

    can produce an XML solution in far less time using Python than he can with Java or C++. Of course, the cross-platform nature of Python keeps our work consistently usable whether we're developing on ...

    Python3中内置类型bytes和str用法及byte和string之间各种编码转换 问题

    Python 3最重要的新特性大概要算是对文本和二进制数据作了更为清晰的区分。文本总是Unicode,由str类型表示,二进制数据则由bytes类型表示。Python 3不会以任意隐式的方式混用str和bytes,正是这使得两者的区分特别...

    googleGson JsonObject json转换包

    包含以下java源文件: ...com.google.gson.JsonNull.class com.google.gson.JsonObject.class com.google.gson.JsonParseException.class com.google.gson.JsonParser.class com.google.gson.JsonPrimitive.class ...

    Python.Scripting.for.ArcGIS.pdf

    Python Scripting for ArcGIS is a guide for experienced users of ArcGIS Desktop to get started with Python scripting without needing previous programming experience. Experience with other scripting or ...

    OpenCV with Python Blueprints

    Readers are expected to be familiar with OpenCV's concepts and Python libraries. Basic knowledge of Python programming is expected and assumed. What You Will Learn Generate real-time visual effects ...

    python实现车道线识别程序

    这个一个python实现的车道线识别程序,基于opencv库。压缩包内还附了测试用的图像和视频,适合进行python和图像处理学习。

    expected time bounds for selection

    expected time bounds for selection

    Python 错误和异常小结

    你还不知道什么是异常,额… 1.Python异常类 Python是面向对象语言,所以程序抛出的异常也是类。常见的Python异常有以下几个,大家只要大致扫一眼,有个映像,等到编程的时候,相信大家肯定会不只一次跟他们照面...

    python实现歌声伴奏分离实验与开发

    This is more or less a wrapper for Matplotlib imaging functions such that their behavior is equivalent, in terms of colormap, aspect and so forth, to the expected behavior of Matlab's functions. ...

    Expert Python Programming.pdf 英文原版.Python专家编程

    Expert Python Programming shows how Python development should be done with best practices and expert design tips.... The reader is expected to have a sound background in Python programming.

    Learning Robotics using Python 2nd

    You're expected to have a basic understanding of Linux commands and Python. Table of Contents Getting started with ROS Understanding basics of differential robots Modeling the Differential Drive ...

    python-2.7.17.amd64.zip

    Python 2.7.17 Release Date: Oct. 19, 2019 Python 2.7.17 is a bug fix release in the Python 2.7.x series. It is expected to be the penultimate release for Python 2.7.

    ubuntu:python调用c生成so文件出错

    bfs.cpp:11:15: error: expected constructor, destructor, or type conversion before ‘(’ token  __declspec(dllexport) windows到linux的转换: windows下: #include #include #include #include using ...

    Python.Hacking.Essentials.1511797568

    Title: Python Hacking Essentials Author: Earnest Wish Length: 214 pages Edition: 1 Language: English Publisher: CreateSpace Independent Publishing Platform Publication Date: 2015-04-01 ISBN-10: ...

Global site tag (gtag.js) - Google Analytics