【问题标题】:Q: TypeError: '_csv.reader' object is not callable问:TypeError:'_csv.reader' 对象不可调用
【发布时间】:2021-03-02 09:03:58
【问题描述】:

我一直收到这个错误,谁能帮我解决这个问题?我在下面突出显示了有问题的代码行。我尝试将 reader 变量更改为 reader1,但问题仍然存在。

错误信息

Traceback(最近一次通话最后一次):

文件“dna.py”,第 20 行,在

读者 = 读者(人物档案)

TypeError: '_csv.reader' 对象不可调用

**

  • 我的代码

**

from sys import argv

from csv import reader, DictReader



if len(argv) < 3:

print("Wrong number of arguments")

exit()



#read the database file into memory

with open(argv[2]) as file:

reader = reader(file)

for row in reader:

dnalist = row



#create a variable containing the sample DNA

dna = dnalist[0]

#create a dictionary that holds the STR and the highest rep count

sequences = {}



with open(argv[1]) as peoplefile:

reader = reader(peoplefile) **<------ ERROR IS HERE**

for row in reader:

dnasequences = row

dnasequences.pop(0)

break

#set the STRs as keys in sequences dictionary

for i in dnasequences:

sequences[i] = 1



#Obtain the highest number of reps of each STR in the given DNA sequence

for key in sequences:

l = len(i)

tmp = 0

tmpmax = 0

#check

for i in dna:

if dna[i: i + l] == key:

tmp = 1

while dna[i:i+l] == dna[ i+l : i+2*l ]:

tmp += 1

i += l

if tmp > tmpmax:

tmpmax = tmp

sequences[key] = tmpmax



#Read database file into a dictionary

with open(argv[1]) as peoplefile:

reader = DictReader(peoplefile)

#loop through STR counts, comparing it to each person's STR counts

for person in reader:

match = 0

for i in sequences:

if sequences[i] == int(person(i)):

match += 1

#if all the highest STR reps match the person, print that person's name

if match == len(sequences):

print(row['name'])

exit()


print("Does not match any person")

【问题讨论】:

    标签: python typeerror cs50


    【解决方案1】:

    当您尝试运行reader = reader(peoplefile) 时,它实际上是从您之前的代码行reader = reader(file) 中获取reader

    换句话说,您的错误行正在尝试运行如下内容:

    reader = reader(file)(reader(peoplefile))

    不要将函数的名称用作变量。使用csv_reader 作为变量而不是仅仅使用reader 可以解决问题。

    ...
    # read the file into memory
    with open(argv[2]) as file:    
        csv_reader = reader(file)    
        for row in csv_reader:    
            dnalist = row
    ...
    with open(argv[1]) as peoplefile:
        csv_reader = reader(peoplefile)
    

    编辑

    我想出了一个例子来更好地解释发生了什么:

    假设您有一个数字要转换为字符串,将其设置为一个名为 str 的新变量,然后打印它。

    x = 279
    str = str(x)
    print(str)
    

    此代码在语法上是正确的,但它可能会忽略代码的后面部分,例如,如果您有另一个数字并且还想将其转换为字符串并打印出来。

    x = 279
    str = str(x)
    print(str)
    
    y = 441
    str = str(y)
    print(str)    # TypeError here
    

    使用与函数同名的变量名将覆盖该函数,并且下次您要调用该函数时实际上是在调用该变量。

    第 2 部分

    换个角度看,我认为例子越多越好:

    只使用import csv 就可以了,所以你必须使用csv.reader() 来调用函数

    import csv
    
    with open('example.txt') as file:
        reader = csv.reader(file)
        for line in file:
            print(line)
        
    with open('second_example_text.txt') as file:
        reader = csv.reader(file)
        for line in file:
            print(line)
    

    在这种情况下,您可以根据需要多次使用reader,因为您不会覆盖函数csv.reader

    希望有帮助!

    【讨论】:

    • 感谢您的解决方案有效。为什么我可以将 csv_reader 用于所有内容,但不能用于阅读器?它不应该与将阅读器作为变量一样工作吗?在我看来,我所做的只是更改变量的名称。
    • 如果您收到通知但我更新了答案,请查看
    猜你喜欢
    • 2019-01-09
    • 2014-02-26
    • 1970-01-01
    • 1970-01-01
    • 2018-12-25
    • 2021-04-15
    • 2011-10-01
    • 2020-11-10
    • 2017-09-09
    相关资源
    最近更新 更多