处理Python请求和JSON响应

处理Python请求和JSON响应,python,arrays,json,unicode,python-requests,Python,Arrays,Json,Unicode,Python Requests,试图处理对API的Python请求调用的一些JSON响应,用Python——我仍在学习的语言 下面是示例返回的JSON数据的结构: {"sports":[{"searchtype":"seasonal", "sports":["'baseball','football','softball','soccer','summer','warm'","'hockey','curling','luge','snowshoe','winter','cold'"]}]} 目前,我正在解析输出并将其写入如下

试图处理对API的Python请求调用的一些JSON响应,用Python——我仍在学习的语言

下面是示例返回的JSON数据的结构:

{"sports":[{"searchtype":"seasonal", "sports":["'baseball','football','softball','soccer','summer','warm'","'hockey','curling','luge','snowshoe','winter','cold'"]}]}
目前,我正在解析输出并将其写入如下文件:

output = response.json
results = output['sports'][0]['sports']
     if results:
          with open (filename, "w") as fileout:
               fileout.write(pprint.pformat(results))
将此作为我的文件:

[u"'baseball','football','softball','soccer','summer','warm'",
"'hockey','curling','luge','snowshoe','winter','cold'"]
因为我基本上是创建双引号JSON数组,由逗号分隔的字符串组成——我如何操作数组以只打印我想要的逗号分隔的值?在本例中,除了表示季节的第五列之外,其他所有内容都将显示

[u"'baseball','football','softball','soccer','warm'",
"'hockey','curling','luge','snowshoe','cold'"]
最后,我也想去掉unicode,因为我没有非ascii字符。我目前在事后用一种我更熟悉的语言(AWK)手动完成这项工作。我期望的输出是:

'baseball','football','softball','soccer','warm'
'hockey','curling','luge','snowshoe','cold'

您的结果实际上是一个字符串列表,要获得所需的输出,您可以这样做,例如:

 if results:
      with open (filename, "w") as fileout:
          for line in results
              fileout.write(line)

是的。你完全正确。这样效果更好。