2

.txt からすべての情報を取得するための Python コードを書いています。そして、私はコードを書きましたが、常にエラーが発生します: 「NoneType」オブジェクトには属性「groupdict」がありません

コード:

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()

助けてください!どうも!

4

1 に答える 1

2

Python ドキュメントから:

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

あなたのコードでは、 re.match は None を返し、それを に保存していmます。そうmですNonegroupdict次にから呼び出しを試みるとm、前述のエラーが発生します。そのため、処理を続行する前に、最初に でmあるかどうかを確認する必要があります。None

于 2012-11-07T00:12:52.803 に答える