无效的语法。我将文本文件保存在桌面上调用它。

时间:2014-11-27 10:43:51

标签: 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 = 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'))

2 个答案:

答案 0 :(得分:2)

你声明你的功能错了:

def loadGraphFileName('file.text'):  # change this
   return loadGraphFile(Open('file.text'))

对此:

def loadGraphFileName():  # You don't use it anyway
   return loadGraphFile(Open('file.text'))

或者:

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

答案 1 :(得分:1)

你不能将文字作为函数参数

你可以改为

def loadGraphFileName(f = 'file.txt'):
   return loadGraphFile(Open(f))