2014-12-07 3 views
0

저는 파이썬 3을 사용하여 파일 읽기 및 사전 연습을하고 있습니다. 파일을 닫으려고하는데 대신 오류가 발생합니다.파일을 닫으려고 할 때 AttributeError 메시지 받기

AttributeError: 'tuple' object has no attribute 'close'. 

코드 :

try: 
ifile = ("inputfile.txt", "r") 
except IOError: 
    print("Error opening file") 
else: 
    for line in ifile: 
     line = line.strip() 
     if not line or line[0] == "#": 
      continue 
     else: 
      data =line.split(" ") 
    mydict = {} 

    for item in data: 
     key = item[0] + item[-1] 

     value = item[1:-1] 
     mydict[key] = [value] 
    print(mydict) 
    ifile.close() 

왜이 오류가 발생하고, 내가 그것을 어떻게 해결할 수 있습니까?

+0

입력 파일에 한 줄이 당신은 단지 tuple은 다음과 같이 변경 만들었습니다? 그런 다음 for-loop가 자극적입니다. 당신은 정말로 당신의 목록에리스트의 목록을 원합니까? – Daniel

답변

3

파일을 열지 않았습니다!

ifile = ("inputfile.txt", "r") 

에 :

ifile = open("inputfile.txt", "r") 
관련 문제