今天看到论坛关于字符串分割问题,见http://www.iteye.com/topic/1117302,看看别人的思路自己用python写了下,竟然报错,list index 越界。
'''
Created on 2011-11-2
@author:dudong0726
'''
if __name__ == '__main__':
str="[Type=UPC-A][Final=10][Nnmber=9]"
for item in str.split("]"):
print(item.split("=")[1])
python分割出来最后有个空串,导致再次对=分割时报错,正确代码如下:
'''
Created on 2011-11-2
@author:dudong0726
'''
if __name__ == '__main__':
str="[Type=UPC-A][Final=10][Nnmber=9]"
for item in str.split("]")[0:-1]:
print(item.split('=')[1])