try:
f = open(blackListPath, ‘r‘)
try:
lines = f.readlines()
blackList = {}
for line in lines:
print type(line)
if isinstance(line, str):
print "string"
else:
print "wrong"
blackList[line] = "black"
if blackList.has_key(line):
print blackList[line]
else:
print "sorry black"
if line == repr("暂停"):
print "yes"
finally:
# print "Error: 没有找到文件或读取文件失败"
f.close()
except IOError:
print "Error: 没有找到文件或读取文件失败"
if blackList.has_key("搜索"):
print blackList[line]
else:
print "搜索"
文件中含有“”搜索”
可是却无法从生成的字典blackList中找出来
最后的if else本应该打印“black”
却打印成了“搜索”
解决:
因为文件每一行含有换行符
需要用line = line.strip(‘\n‘)去掉换行符!!!
原文:http://www.cnblogs.com/ilovewhaley/p/6363014.html