【问题标题】:neo4j python language driverneo4j python语言驱动
【发布时间】:2018-09-25 20:12:09
【问题描述】:

我正在尝试来自 neoj4 网站的 Python 驱动程序示例。问题是我不断收到 IndentionError

我尝试过空格和制表符。没有解决它。在这个简单的文件中,我做了一个:

def print_this(str):
  print(str)
  return;

print_this('a simple test')

效果很好。

from neo4j.v1 import GraphDatabase

uri = "bolt://localhost:7687"
driver = GraphDatabase.driver(uri, auth=("neo4j", "password"))

def print_friends_of(tx, name):
    for record in tx.run("MATCH (a:Person)-[:KNOWS]->(f) "
                         "WHERE a.name = {name} "
                         "RETURN f.name", name=name):
    print(record["f.name"])

with driver.session() as session:
    session.read_transaction(print_friends_of, "Alice")

错误

print(record["f.name"])
    ^
IndentationError: expected an indented block

有人知道吗?

坦克

【问题讨论】:

  • 您的“for”语句和“print”语句处于同一级别,这就是您收到该错误的原因。因为“print”语句应该在“for”里面,所以它会给你这个错误。
  • 好点,但它不起作用。比我得到一个语法错误。该示例来自 Neoj4 官方网站。我确实假设他们已经测试过了。 neo4j.com/docs/api/python-driver/current 但我无法让它工作。
  • 正确缩进打印后会出现什么错误?缩进更改后我没有收到任何错误。

标签: python neo4j


【解决方案1】:

附言

for record in tx.run("MATCH (a:Person)-[:KNOWS]->(f) "
                     "WHERE a.name = {name} "
                     "RETURN f.name", name=name):

您正在开始一个新的for 循环。您的下一行应该缩进,因此它位于 for 循环内。改成

for record in tx.run("MATCH (a:Person)-[:KNOWS]->(f) "
                         "WHERE a.name = {name} "
                         "RETURN f.name", name=name):
    print(record["f.name"]) #note how it is indented to be inside of the for loop

【讨论】:

  • 谢谢你解决它;-)
猜你喜欢
  • 2022-12-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-29
  • 2012-07-23
  • 2013-01-10
  • 2020-01-01
  • 1970-01-01
相关资源
最近更新 更多