小编ada*_*dam的帖子

通过嵌套json对python中的特定键进行递归迭代

我正在尝试从json文件中提取嵌套值.我想打印出每个"id"键的每个值.我认为我很接近,但无法弄清楚为什么obj类型从dict变为列表,然后为什么我无法解析该列表.这是我正在使用的json的链接:http://hastebin.com/ratevimixa.tex

这是我目前的代码:

#!/usr/bin/env python
#-*- coding: utf-8 -*-

import json

json_data = open('JubJubProductions.json', 'r+')
jdata = json.loads(json_data.read().decode("utf-8"))

def recursion(dict):

    for key, value in dict.items():

        if type(value) == type(dict):
            if key != "paging":
                for key, value in value.items():
                    if isinstance (value,list):
                        print key
                        # place where I need to enter list comprehension?
                if type(value) == type(dict):
                    if key == "id":
                        print " id found " + value
                    if key != "id":
                        print key + " 1st level"
                if …
Run Code Online (Sandbox Code Playgroud)

python iteration recursion json list-comprehension

16
推荐指数
3
解决办法
3万
查看次数

如何在neo4j中使用cypher获取节点属性列表

我正在尝试列出一组节点的所有属性.

Match (n:"Indicator")
return properties(n), ID(n) 
Run Code Online (Sandbox Code Playgroud)

我不确定语法,无法在refcard或docs中找到答案.

neo4j cypher

10
推荐指数
2
解决办法
7343
查看次数

在 Python 中使用 mysql.connector 处理格式参数失败

我无法弄清楚我在这个插入语句中做错了什么。我得到的错误是:

 "Failed processing format-parameters; %s" % err)
mysql.connector.errors.ProgrammingError: Failed processing format-parameters; 
'MySQLConverter' object has no attribute '_navigablestring_to_mysql'`
Run Code Online (Sandbox Code Playgroud)

具体的代码行是:

update = '''INSERT INTO myDB.newtable (ID,Record,Latitude,Longitude,code) VALUES (%s,%s,%s,%s,%s)'''
cursor2.execute(update,(ID,Record,Latitude,Longitude,code))
cnx2.commit()
Run Code Online (Sandbox Code Playgroud)

我也试过这种格式:

update = ("INSERT INTO myDB.newtable (ID,Record,Latitude,Longitude,code) VALUES (%s, %s, %s, %s, %s)")%(ID,Record,Latitude,Longitude,code)
cursor2.execute(update)
Run Code Online (Sandbox Code Playgroud)

并收到此错误: mysql.connector.errors.ProgrammingError: 1054 (42S22): Unknown column '45676kb' in 'field list'

45676kb只是整个价值的一部分。完整的字符串是45676kb-98734-98734-123nn.

我认为第二次尝试的语法更正确,因为我至少收到了一个 sql 错误,但我不知道如何使用 mysql.connector 正确格式化我的插入语句。

python mysql

5
推荐指数
1
解决办法
1万
查看次数

在新行上输出json对象组而不是单行

我正在加载一个json文件并尝试拉取一些值然后按行输出该组值.

当前输出如下所示:

{"time":"1:2:2","post":"1","user":"4","text":"Masaru Emoto"}{"time":"1:3:8","post":"8","user":"5","text":"Meteors"}{"time":"7:4:5","post":"1","user":"8","text":"Olympics"}
Run Code Online (Sandbox Code Playgroud)

我希望它看起来像这样:

{"time":"1:2:2","post":"1","user":"4","text":"Masaru Emoto"}
{"time":"1:3:8","post":"8","user":"5","text":"Meteors"}
{"time":"7:4:5","post":"1","user":"8","text":"Olympics"}
Run Code Online (Sandbox Code Playgroud)

我无法弄清楚在哪里添加"\n"来修复输出.在此先感谢您的帮助.

码:

import json

json_data = open('somefile.json', 'r+').read().decode("utf-8")
jdata = json.loads(json_data)

def id_generator(d):    

with open('formatted_file' + '.json', 'w') as outfile:
    for k, v in d.items():
        if isinstance(v, dict):
            id_generator(v)                    
        if isinstance(v, list):            
            for post in v:
                formated = {"time":post.get('created_time'),"user":post['from']['id'],
                               "post":post.get('id'),"text":post.get('description')}                                        
                out = json.dumps(formated, separators=(',', ':'))                                                         
                outfile.write(out)                                        
if __name__ == '__main__':
    try:
        id_generator(jdata)
    except TypeError:
        pass 
Run Code Online (Sandbox Code Playgroud)

python arrays json newline object

3
推荐指数
1
解决办法
1万
查看次数