【发布时间】:2017-07-24 17:10:20
【问题描述】:
我正在解析一些 JSON(特别是亚马逊公开提供的亚马逊评论文件)。我正在逐行解析并转换为 Pandas DataFrame 并动态插入 SQL。我发现了一些非常奇怪的东西。我使用 UTF-8 打开 json 文件。当我用记事本打开文件本身时,我看不到任何奇怪的符号或其他任何东西。比如review的子串:
The temperature control doesn’t hold to as tight a temperature as some of the others reported.
但是当我解析它并检查字符串的内容时:
The temperature control doesn\xe2\x80\x99t hold to as tight a temperature as some of the others reported.
为什么会这样?我怎么看不懂?
我当前的代码如下:
def parseJSON(path):
g = io.open(path,'r',encoding='utf8')
for l in g:
yield eval(l)
for l in parseJSON(r"reviews.json"):
for review in l["reviews"]:
df = {}
df[l["url"]] = review["review"]
dfInsert = pd.DataFrame( list(df.items()), columns = ["url", "Review"])
失败的文件子集在那里: http://www.filedropper.com/subset
【问题讨论】:
-
你为什么用
eval而不是json.loads(...)?那可能是你的问题。还有python2还是python3? -
@AnthonySottile 它是 2.7.13 |Continuum Analytics, Inc. 使用 json.loads() 结果完全相同
-
文件是怎么写的?
-
doesn’t中的撇号不是普通的单引号'==\x27,它是右单引号 ==\u2019。 -
如果你想去掉花哨的引号等并用它们的纯 ASCII 等价物替换它们,看看Unidecode。最好让您的程序正确处理 Unicode,但这并不总是一种选择。
标签: python json encoding utf-8