在string文件中search'str'types的数组
我在non_int_ram_var
中searchnon_int_ram_var
中的trial.txt
。 non_int_ram_var
从基本上unicode的excel表中获取数据。 因此,我将其types转换为str
,以使trial.txt
types与non_int_ram_var
匹配。 但是,控制是不是进入if var in line:
。 我已经交叉validationvar
是否出现在文本文件的一行中。 我可能会犯一些错误。 任何人都可以给我一个build议吗?
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()
================================================== ================
在阅读到下一次迭代文件结束之后,光标不会到达文件的开头。 这是我的错误。 谢谢戳,你的build议显示的方式。 这是我现在做的,工作正常。
如果不行:map_file.seek(0)break
Python默认将文件读入str对象。
我想你可以试试
for line in map_file: if var in line: print "control here" ...