Python 无效语法。我将文本文件保存在桌面上,称之为文件。
创建图形:-Python 无效语法。我将文本文件保存在桌面上,称之为文件。,python,Python,创建图形:- def loadGraphFile(file): graph = [] for line in file: contents = line.split() movieName = contents[0] actorNames = [contents[i]+ " " + contents[i+1] for i in range(1, len(contents), 2)] movieNode
def loadGraphFile(file):
graph = []
for line in file:
contents = line.split()
movieName = contents[0]
actorNames = [contents[i]+ " " + contents[i+1] for i in range(1, len(contents), 2)]
movieNode = findNode(graph, movieName)
if movieNode == None:
movieNode = mkNode(movieName)
graph.append(movieNode)
for actorName in actorNames:
actorNode = findNode(graph,actorName)
if actorNode == None:
actorNode = mkNode(actorName)
graph.append(actorNode)
actorNode.neighbor.append(movieNode)
movieNode.neighbor.append(actorNode)
return graph
def loadGraphFileName('file.text'):
return loadGraphFile(Open('file.text'))
不能将文字作为函数参数 你可以这样做
def loadGraphFileName(f = 'file.txt'):
return loadGraphFile(Open(f))
您声明您的函数错误:
def loadGraphFileName('file.text'): # change this
return loadGraphFile(Open('file.text'))
为此:
def loadGraphFileName(): # You don't use it anyway
return loadGraphFile(Open('file.text'))
或:
请显示准确的错误消息。您确定有一个名为
Open
的函数吗?它不应该是打开的
(全部小写)?下面是编译我的项目行37 def loadGraphFileName('file.text')时显示的错误:^SyntaxError:无效语法感谢它修复了我的无效语法错误,但它没有读取我的文本file@JamesSmith你的Python程序在哪里,它和file.txt在同一个地方吗?在我的file.txt中显示了一些著名演员的名字。。。我需要创建一个主节点吗method@JamesSmithpy程序和file.txt的位置必须是sameI在CANOPY上编写python代码的位置。。我是Python的新手,但是当我编写java或C++时,我可以很容易地把我的文本文件拖进项目中…但是我该如何为python做到这一点呢?谢谢,它确实修复了我的无效语法错误,但它没有读取我的文本文件。您的文本文件的确切名称是什么?它可能是txt而不是文本。它们也在同一个目录中吗?当我运行程序时,我遇到一个错误:%run“C:/Users/*********/Desktop/KB2.py”你能把完整的堆栈跟踪放在问题中吗?
def loadGraphFileName(filename='file.text'): # file.text will be the default. if you give an parameter with it, filename will change to that parameter
return loadGraphFile(Open(filename)) # And use it here