【问题标题】:Is there a way to store dynamic Cypher query into python variable?有没有办法将动态 Cypher 查询存储到 python 变量中?
【发布时间】:2021-03-14 23:51:55
【问题描述】:

有没有办法将我的密码查询放入 python 变量中?我可以将密码的非参数放入变量中,但出现错误 [![enter image description here][1]][1]

[1]: https://i.stack.imgur.com/mfRrp.png 尝试将参数部分放入变量中。

我正在使用 pandas 和 py2neo 将 CSV 加载到 neo4j 中。 df_data 是我的 pandas 数据框,有 2 列和 3 行。

for index,row in df_data.iterrows():
        tx.evaluate('''MERGE (x:PATIENT_ID {property:$PATIENT_ID})
            MERGE(y:GLB_ID {property:$GLB_ID})
            MERGE (x)-[r:R_TYPE]->(y) ''', parameters = {'PATIENT_ID': int(row['PATIENT_ID']), 'GLB_ID': int(row['GLB_ID'])})

如果我运行以下将非参数部分存储到 cq 中的代码,我的代码运行没有问题:

for index,row in df_data.iterrows():
        tx.evaluate(cq, parameters = {'PATIENT_ID': int(row['PATIENT_ID']), 'GLB_ID': int(row['GLB_ID'])})

我正在寻找一种将参数部分存储到 python 变量中的方法

【问题讨论】:

  • 所以您想从数据帧中获取键名:PATIENT_ID 和 GLB_ID 并将其用作传递给 neo4j 的参数字典的键?

标签: python-3.x pandas dataframe neo4j py2neo


【解决方案1】:

我使用字典来解决这个问题。我使用字典而不是密码查询来解决它:

for index,row in df_data.iterrows():
        t_dict = {}
        for k in p_dict.keys():
            try: 
                t_dict[k] = int(row[k])
            except TypeError:
                t_dict[k] = row[k]
        tx.evaluate(cq,t_dict)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-31
    • 2017-12-15
    • 2021-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多