python执行报错
- Python code
import res = 'skdfhlaknhl'a = re.findall(r'(\d+\.\d+\.\d+\.\d+)', s)print aif a != []: a = selse : a = a[0]print a
执行报错:
C:\Python27\python.exe D:/python_dev/untitled/10s.py
[]
Traceback (most recent call last):
File "D:/python_dev/untitled/10s.py", line 8, in <module>
a = a[0]
IndexError: list index out of range
求解。
[解决办法]
- Python code
import MySQLdbimport reip_patt = re.compile(r'(\d+\.\d+\.\d+\.\d+)')im_file=r'/20120528.txt'def ln_parser(ln): p4, s=line.split(';')[3:4] match = ip_patt.match(s) a = '' if not match else match.group(1) return (p4, a)todo = list(set(map(ln_parser, open(im_file,'r'))))conn = MySQLdb.Connect(host="localhost",user="root",passwd="1111",db="111")conn.executemany("insert into cbd.domain values (%s,%s)",todo)conn.commit()conn.close()