2013-09-25 36 views
0

獲取文件爲什麼我得到錯誤脅迫到Unicode:需要字符串或緩衝區,詮釋發現從用戶

file = input("Please enter the data file you wish to open: ") 

獲取搜索值從用戶

value = int(input("Input a number to search for: ")) 

打開文件

datlist = open(file, "rb") 

閱讀文件中的行

FileNums = datlist.read().splitlines() 

關閉文件

datlist.close() 
+0

請複製粘貼您在運行上述腳本時得到的確切錯誤消息。在*我*機器上,我無法獲得您在問題中暗示的錯誤信息。 –

+0

如果你可以標記答案,如果它幫助你解決你的問題,那將是很好的。 – jbaiter

回答

0

第一個File是python中的一個保留字。所以你最好不要把它用作變量。 這裏是一個樣本作爲參考

#!/bin/python 

filename = raw_input("Please enter the data file you wish to open: ") 
value = raw_input("Input a number to search for: ") 

fin = file(fin, "rb") 

for lines in fin: 
    line = lines.rstrip().split() 
    if value in line: 
     print line 

fin.close() 
+0

保留關鍵字是'file',而不是'File'。 – Matthias

相關問題