【发布时间】:2014-02-24 12:47:18
【问题描述】:
我在trial.txt 中搜索non_int_ram_var 中的文本。 non_int_ram_var 从基本上是 unicode 的 excel 表中获取数据。所以我将它类型转换为str,以便trial.txt 类型与non_int_ram_var 匹配。但是控件没有进入if var in line:。我已经交叉验证 var 存在于文本文件的其中一行中。我可能会犯一些错误。谁能给我一个建议?
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) 打破
【问题讨论】:
-
在检查 if 条件之前请检查
line和var的值是多少?如果您在unicode中检查str,那么它将起作用。
标签: python string excel search unicode