I’m writing a python code to slipt all informations from a .txt. And i have write a code, but come allways error: ‘NoneType’ object has no attribute ‘groupdict’
code:
import re
readfile = open("test.txt")
try:
all_the_text =readfile.read()
m = re.match("(pdflink=[\w\s/]*)author=([\w\s/]*/n)",unicode(all_the_text),flags=re.UNICODE)
m.groupdict()
print m.groupdict()
finally:
readfile.close()
writefile = open('test5.txt','w')
print >> writefile, m.groupdict()
writefile.close()
Please please help me! Thx!
From the Python documentation:
re.match(pattern, string, flags=0)¶
If zero or more characters at the beginning of string match the regular expression pattern, return a corresponding MatchObject instance. Return None if the string does not match the pattern; note that this is different from a zero-length match
In your code, re.match is returning None and storing that in
m. SomisNone. You then try to callgroupdictfromm, and you get the aformentioned error. So you should first check to see ifmisNonebefore continuing your processing.