2014-02-24 65 views
1

我正在尋找中的文本trial.txtnon_int_ram_var從基本上unicode的excel表格中獲取數據。因此,我將其輸入到str,以便trial.txt類型與non_int_ram_var匹配。但控制權不會進入if var in line:。我已經交叉驗證var存在於文本文件的一行中。我可能會犯一些錯誤。任何人都可以給我一個建議嗎?在字符串文件中搜索'str'類型的數組

from xlrd import open_workbook 
import unicodedata 
work_book= open_workbook("C:\\Users\\E542639\\Desktop\\non_sram_mem\\SEU_MBU_FINAL_VARIABLE_LIST.xls"); 

# reading xls file for non_int sram.............. 
non_int_ram_var = [] * 376 
row=1 

for sheet in work_book.sheets(): 
    if "Data" == sheet.name : 
     print sheet.nrows, sheet.ncols 
     while row < sheet.nrows: 
      if "int_sram" != sheet.cell(row,5).value: 
       if "file name only" != sheet.cell(row,7).value : 
        non_int_ram_var.append(str(sheet.cell(row,1).value)) 
      row=row + 1 

# checking variable in mapfile......... 

map_file = open("C:\\Users\\E542639\\Desktop\\non_sram_mem\\trial.txt", "r") 
non_categorized = open("C:\\Users\\E542639\\Desktop\\non_sram_mem\\non_categorized.txt","w") 

print "processing..." 

for var in non_int_ram_var: 
    while True: 
     line = str.encode(map_file.readline()) 
     if not line: break 

     if var in line: 
      print "control here" 
      non_categorized.writelines(line) # write won't work 

print 'done!!!' 

map_file.close() 
non_categorized.close() 

=========================================== =======================

在讀取到下一次迭代文件結束之後,光標不會到達文件的開頭。這是我的錯誤。感謝戳,你的建議顯示的方式。這是我現在做的並且工作正常。

如果不是行: map_file.seek(0) 斷裂

+0

請你檢查什麼是'line'和'var'的,如果條件檢查前值?如果你在'unicode'中檢查'str',那麼它就會工作。 – Nilesh

回答

0

的Python文件讀入到STR對象默認情況下。

我想你可以嘗試

for line in map_file: 
    if var in line: 
     print "control here" 
     ... 
+0

在讀完之後直到下一次迭代文件結束時,光標不會到達文件的開頭。這是我的錯誤。感謝戳,你的建議顯示的方式。這是我現在做的並且工作正常。 if not line:map_file.seek(0)break –