2013-11-26 14 views
0

所以我想寫一段代碼從文件中取文本,移入字典,然後處理它。我不斷收到此錯誤:文件到字典,不能讓程序運行

File "C:\Users\Oghosa\Assignment2.py", line 12, in <module> 
builtins.IndexError: string index out of range 

這是我的計劃:

endofprogram = False 
dic = {} 
try: 
    filename = input("Please Enter the Filename:") 
    infile = open(filename, 'r') 
except IOError: 
    print("Error Reading File! Program ends here!") 
    endofprogram = True 
if endofprogram == False: 
    for line in infile: 
     line = line.strip("\n") 
     if (line != " ") and (line[0] != "#"): 
      item = line.split(":") 
      print(items) 
      dic["Animal id"] = item[0] 
      dic["Date"] = item[1] 
      dic["Station"] = item[2] 
     print(dic) 

可以指出我的錯誤,請人援助?

以下是樣本輸入文本:

#Comments 
a01:01-24-2011:s1 
a03:01-24-2011:s2 
    <blank line> 
    <blank line> 
a02:01-24-2011:s2 
a03:02-02-2011:s2 
a03:03-02-2011:s1 
a02:04-19-2011:s2 
    <blank line> 
#comments 
a01:05-14-2011:s2 
a02:06-11-2011:s2 
a03:07-12-2011:s1 
a01:08-19-2011:s1 
a03:09-19-2011:s1 
a03:10-19-2011:s2 
a03:11-19-2011:s1 
a03:12-19-2011:s2 
+0

你能給我們一個輸入文件樣本嗎? – Christian

+0

@Christian這裏有一個叫animallog.txt – user2767528

+0

不要把它作爲評論發佈,編輯你的帖子並把它放在那裏。確保格式正確。 – Christian

回答

0

你有沒有在你的文件中的空行?在第12行,您可能需要在使用line[0]對其進行索引之前檢查該行是否有文本。你是否真的有一行應該是12行的空串:

if line.strip() and (line[0] != "#"): 

編輯..添加一個完整的例子。

dic = {} 
filename = input("Please Enter the Filename:") 
try: 
    infile = open(filename, 'r') 
except IOError: 
    print("Error Reading File! Program ends here!") 
else: 
    for line in infile: 
     line = line.strip() 
     if line and line[0] != "#": 
      item = line.split(":") 
      dic["Animal id"] = item[0] 
      dic["Date"] = item[1] 
      dic["Station"] = item[2] 
+0

那裏有空字符串, 。我已經在原始文章中指定了它們。謝謝 – user2767528

+0

我修改了我的答案,在truthy檢查之前將空白字符串串起來。 – dstanek

+0

謝謝。修復它,我可以問爲什麼我以前的代碼不工作> – user2767528

0

那麼,你應該至少打印出錯行,所以你知道罪魁禍首是什麼:

for line in infile: 
    items = line.strip("\n") 
    try: 
     if (line.strip != "") and (items[0] != "#"): 
      items = line.split(":") #i dont like your reuse of line so changing to items 
      .... 
    except IndexError: #if python 3 use except IndexError as e: 
     print(items) #prints offending line 
0
endofprogram = False 
    attrs=["Animal id","Date","Station"] 
    dictionary=[] 
    try: 
     # filename = input("Please Enter the Filename:") 
     infile = open('rite.txt', 'r') 
    except IOError: 
     print("Error Reading File! Program ends here!") 
     endofprogram = True 
    if endofprogram == False:  
     for line in infile: 
      line = line.strip("\n") 

      if (line != "") and (line[0] != "#"): 
       item = line.split(":") 
       dictionary.append(dict(zip(attrs, item))) 
    print dictionary 
0

你的問題是,當有空白行,在該文件中,line[0]沒有按不存在。爲了解決這個問題,試試這個版本:

endofprogram = False 
dic = {} 
try: 
    filename = input("Please Enter the Filename:") 
    infile = open(filename, 'r') 
except IOError: 
    print("Error Reading File! Program ends here!") 
    endofprogram = True 
if endofprogram == False: 
    for line in infile: 
     line = line.strip("\n") 
     if len(line): 
      if line[0] != "#": 
       item = line.split(":") 
       print(items) 
       dic["Animal id"] = item[0] 
       dic["Date"] = item[1] 
       dic["Station"] = item[2] 
      print(dic) 

另外值得注意的是,你在每次循環覆蓋dic。所以在循環完成之後; dic將僅包含文件最後一行的信息。

0

問題是你沒有在

if (line != " ") and (line[0] != "#"): 

語句檢查空行正確。這是因爲在執行line = line.strip("\n")之後他們甚至沒有剩餘空間,所以幾乎任何索引操作都會失敗。

下面的代碼有固定的和其他一些編碼錯誤。請注意,在此處發佈實際代碼非常重要,以便人們可以更輕鬆地爲您提供幫助。

endofprogram = False 
dic = {} 
try: 
    filename = input("Please Enter the Filename:") 
    infile = open(filename, 'r') 
except IOError: 
    print("Error Reading File! Program ends here!") 
    endofprogram = True 
if not endofprogram: 
    for line in infile: 
     line = line.strip("\n") 
     if line and line[0] != "#": 
      items = line.split(":") 
      print(items) 
      dic["Animal id"] = items[0] 
      dic["Date"] = items[1] 
      dic["Station"] = items[2] 
    print(dic)