-1

我是python的初学者,我刚刚在python中创建了一个模块文件,其中包含几个函数。当我调用文件中定义的第一个函数时,它很好。但是当我尝试调用第二个函数时,它说:

AttributeError: 'module' object has no attribute 'file2file'(file2file是我自己定义的一个函数)

这是文件的代码

import sys
import scipy as sci
import scipy.sparse as sp
import numpy as np

def file2map(inf):
    dic = dict()
    with open(inf, "r") as fin:
        for line in fin:
            s = line.split("\t")
            dic[(int(s[0]),int(s[1]))] = float(s[2])
    return dic

def file2file(inf,outf):
    with open(inf, "r") as fin:
        with open(outf, "w") as fout:
        for line in fin:
            s = line.split("\t")
            fout.write("t{0}\t{1}\t{2}\n",s[0],s[1],s[2])

该文件的名称是dataprocessing.py,当我键入时dataprocessing.file2map('xxx.data'),它很好,但是AttributeError: 'module' object has no attribute 'file2file'在我键入时生成了一条错误消息dataprocessing.file2file('xxx.data','out.data')

非常感谢!

4

1 回答 1

0

也许这不是导致您的错误的原因,但我看到您在最内部的 for 循环中错过了一个缩进file2file(inf,outf)

它应该是:

def file2file(inf,outf):
    with open(inf, "r") as fin:
        with open(outf, "w") as fout:
            for line in fin:
                s = line.split("\t")
                fout.write("t{0}\t{1}\t{2}\n",s[0],s[1],s[2])
于 2013-07-13T15:11:56.593 回答