读者未定义[Python]

时间:2017-10-25 19:02:26

标签: python

我正在计算不同类型的属性之间的距离。

在下面的代码中,当我为5-6个元组执行它时,它工作正常,但是当我通过读取.csv文件来创建错误时。

请告诉我有什么问题

Error:

Traceback (most recent call last):
File "bank.py", line 91, in <module>
a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q = [DataItem(z) for z in reader]
NameError: name 'reader' is not defined

我不知道为什么读者突然得到了不确定。?

我也对我们创建的对象进行了安静的研究,因为文件阅读器在这种情况下代表“读者”。 这个读者代表什么/如何?

我对对象的了解就像在OOP中一样,我们为一个方法或类创建它,并使用它在范围内的任何地方进行操作,在python中它是一个迭代文件(外部)的对象。

这个读者对象是什么,对我来说仍然是个问题。 它代表完整的文件吗? 可以在对象上做一个循环吗?访问文件元素?

EDIT1 ::我是python的新手

EDIT2 ::我还根据讨论更新了缩进,但无法运行输出。

新错误:

Traceback (most recent call last):
File "bank.py", line 116, in <module>
main()
File "bank.py", line 93, in main
a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q = [DataItem(z) for z in reader]
File "bank.py", line 56, in __init__
self.values = [type_(value) for type_,value in zip(self.types, values)]
ValueError: invalid literal for int() with base 10: 'age;"job";"marital";"education";"default";"balance";"housing";"loan";"contact";"day";"month";"duration";"campaign";"pdays";"previous";"poutcome";"y"'

import CSV

class NominalType:
    name_values = {}   


def __init__(self, name):
    self.name = name
    self.value = self.name_values[name]

def __str__(self):
    return self.name

def __sub__(self, other):
    assert type(self) == type(other), "Incompatible types, subtraction is undefined"
    return self.value - other.value




def make_nominal_type(name_values):
    try:
        nv = dict(name_values)
    except ValueError:
        nv = {item:i for i,item in enumerate(name_values)}




class MyNominalType(NominalType):
    name_values = nv
return MyNominalType



job = make_nominal_type(["unemployed", "services", "management", "blue-collar"])
contact = make_nominal_type(["cellular", "unknown"])
month = make_nominal_type(["oct", "may", "apr", "jun"])
outcome = make_nominal_type(["unknown", "faliure"])
y = make_nominal_type(["no", "yes"])



class MixedVectorType:
    types = []          
    distance_fn = None  

def __init__(self, values):
    self.values = [type_(value) for type_,value in zip(self.types, values)]

def dist(self, other):
    return self.distance_fn([abs(s - o) for s,o in zip(self.values, other.values)])



def make_mixed_vector_type(types, distance_fn):
    tl = list(types)
    df = distance_fn

class MyVectorType(MixedVectorType):
    types = tl
    distance_fn = df
return MyVectorType


def euclidean_dist(_, vector):
    return sum(v*v for v in vector) ** 0.5
DataItem = make_mixed_vector_type(
[int, job, marital, education, default, int, housing, loan, contact, int, month, int, int, int, int, outcome, y],
euclidean_dist
)
def main():

    with open('bank.csv', 'rb') as csvfile:
         reader=csv.reader(csvfile)

         a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q = [DataItem(z) for z in reader]

         print("a to b, dist = {}".format(a.dist(b)))
         print("b to c, dist = {}".format(b.dist(c)))
         print("c to d, dist = {}".format(c.dist(d)))
         print("d to e, dist = {}".format(d.dist(e)))
         print("e to f, dist = {}".format(e.dist(f)))
         print("f to g, dist = {}".format(f.dist(g)))
         print("g to h, dist = {}".format(g.dist(h)))
         print("h to i, dist = {}".format(h.dist(i)))
         print("i to j, dist = {}".format(i.dist(j)))
         print("j to k, dist = {}".format(j.dist(k)))
         print("k to l, dist = {}".format(k.dist(l)))
         print("l to m, dist = {}".format(l.dist(m)))
         print("m to n, dist = {}".format(m.dist(n)))
         print("n to o, dist = {}".format(n.dist(o)))
         print("o to p, dist = {}".format(o.dist(p)))
         print("p to q, dist = {}".format(p.dist(q)))




if __name__=="__main__":
    main()

0 个答案:

没有答案