2017-02-08 10 views
0

私は質問があります。私は、テキストファイルから2つの部分文字列の間に特定の文字列を取得しようとしています。私は次の「asitename」との間でテキストに興味を持って、今Python:2つの文字列の間にテキストを取得

r\n[url=asitename][/url]\r\n[url=x]hello[/url]\r\n[url=x]a text[/url]\r\n[url=n]test[/url]\r\n\ttest
\r\n\t\t\r\n\t\twarn user\r\n\t\r\n\t2\r\n\t02-06-2017, 01:44 am
by cinccino. end: modcp_reports_report -->

「末端:」

私がしたい

テキストファイルは大きいが、その一部は、これを含んでいますそれらの文字列の間のテキストを取得します。

私はこれを試してみました:

lookup = 'asitename' 
myFileread = myFile.read() 
re.search(lookup+'(.*)end', myFileread).group(1) 

しかし、それは次のエラーが返されます。

AttributeError: 'NoneType' object has no attribute 'group'

私はこれが動作しない理由として混乱しています。よりシンプルな文字列で動作します。

+1

あなたが望む出力を共有することはできますか? –

答えて

0

Return None if no position in the string matches the pattern; note that this is different from finding a zero-length match at some point in the string.

.search()が返すようです。 Python APIによると、これはパターンにマッチする位置がないことを意味します。

1

.も改行文字(複数可)と一致するようになりますre.Sフラグを使用します。

>>> re.search(r'asitename(.*?)end', myFileread, re.S).group(1) 
'][/url]\r\n[url=x]hello[/url]\r\n[url=x]a text[/url]\r\n[url=n]test[/url]\r\n\ttest\n\r\n\t\t\r\n\t\twarn user\r\n\t\r\n\t2\r\n\t02-06-2017, 01:44 am\nby cinccino. 
+0

残念ながら、これは同じエラーを返します。 – hellobro

+0

'myFileread'が正しい文字列であることを確認してください –

関連する問題