2013-04-29 37 views
1

我在Python中相當新。 我解析一個大文件,我想檢查不同的輸入是否正確,尤其是輸入的ID是否在文件頭中。 當我運行下面的代碼,我收到此錯誤信息:我的Python代碼中的AttributeError

AttributeError的: '海峽' 對象有沒有屬性 'readlines方法'


filename = str(raw_input('enter filename: ')) 

try: 
    with open(filename, 'rU'): pass 
except IOError: 
    print 'The file does not exist' 
    sys.exit(0) 

def findID(w): 
    return re.compile(r'\b({0})\b'.format(w), flags=re.IGNORECASE).search 

while True: 
    ID = (raw_input("Enter ID: ")).upper() 
    IDheader = ID + ".NA" 
    with open(filename, 'rU') as f: 
     first_line = f.readline() 
    if findID(IDheader)(first_line): 
     print "you entered ",ID 
     break 
    else: 
     pass 
     print "ID not in this file."` 


for line in filename.readlines(): 
    Line = line.split() 

    if... 

謝謝

回答

0

filename是一個文件名,而不是文件句柄。你需要打開它:

with open(filename, 'r') as handle: 
    for line in handle: 
     line = line.split() 
+0

謝謝,這使它。 – Thierry 2013-04-29 22:43:44