이 오류가 계속 발생합니다. 누구든지 해결할 수 있습니까? 아래에서 문제가되는 코드 줄을 강조 표시했습니다. 독자 변수를 reader1로 변경하려고 시도했지만 문제가 지속됩니다.
에러 메시지
역 추적 (가장 최근 호출 마지막) :
파일 "dna.py", 20 행, in
독자 = 독자 (peoplefile)
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")
실행하려고 할 때 reader = reader(peoplefile)
실제로 reader
이전 코드 줄에서 가져 옵니다 reader = reader(file)
.
즉, 오류 줄이 다음과 같이 실행하려고합니다.
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
도움이 되었기를 바랍니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다