XMLファイルがあります。そのファイルには、htmlタグを含む多くのtwitが含まれています。他のすべてのタスクの中 は、私は次のコードを書かれている@emoji 言葉ですべてのタグを交換する必要があります:それは完全にラインのために働いているすべての<img>タグをXMLファイルの1つの単語に置き換えてください。
for word in re.findall(r"<img[\w\W]+?/>",line):
print word
line = line.replace(word,'@emoji')
。 しかし、私がファイル全体のループでそれをしようとすると、このループの中には入りません。ここでは、コードは次のようになります。
import re
import xml.etree.ElementTree as ET #xml lib
filename = 'da0d0e3527b931bb0bc6f5435003ea2a.xml'
tree = ET.parse(filename)
root = tree.getroot()
twits = []
for child in root:
for grandchild in child:
twits.append(grandchild.text)
for line in twits:
for word in re.findall(r"<img[\w\W]+?>",line):
line = line.replace(word,'@img')
print line
私もHTMLパーサと同じことをしようとしていたが、私は、文字列にタグをオンにすることはできません。
imgs = soup.find_all('img')
for img in imgs:
print img
emo = str(img)
twit.replace(emo,'@emoji')
xmlファイルを、それを公開することは非常に大きな完了しましたが、次のようになります。
<author>
<documents>
<document id="396228853267714048" url="https://twitter.com/ReissSudden/status/396228853267714048">Sooooo many slutty cats knocking around last night</document>
<document id="396229373554360320" url="https://twitter.com/ReissSudden/status/396229373554360320"><a href="/AndyLee666" class="twitter-atreply pretty-link js-nav" dir="ltr" data-mentioned-user-id="259958055" ><s>@</s><b>AndyLee666</b></a> yep, eye hurts but doesn&#39;t look bad ha ha</document>
<document id="396326071467270144" url="https://twitter.com/ReissSudden/status/396326071467270144">Time to start saving for a Skyline</document>
<document id="396326916372054016" url="https://twitter.com/ReissSudden/status/396326916372054016">@LaurenWeale where were your halo and wings then?</document>
<document id="396327202260017152" url="https://twitter.com/ReissSudden/status/396327202260017152">@LaurenWeale I didn&#39;t see them, and besides, it&#39;s not a scary costume</document>
<document id="396327842252075008" url="https://twitter.com/ReissSudden/status/396327842252075008">@LaurenWeale ahh beat me to it <img class="Emoji Emoji--forText" src="https://abs.twimg.com/emoji/v2/72x72/1f609.png" draggable="false" alt="😉" title="Winking face" aria-label="Emoji: Winking face"></document>
<document id="396328213074677763" url="https://twitter.com/ReissSudden/status/396328213074677763">The best chair ever! <a href="/hashtag/halloween?src=hash" data-query-source="hashtag_click" class="twitter-hashtag pretty-link js-nav" dir="ltr" ><s>#</s><b>halloween</b></a> <a href="/hashtag/Throne?src=hash" data-query-source="hashtag_click" class="twitter-hashtag pretty-link js-nav" dir="ltr" ><s>#</s><b>Throne</b></a> <a href="/hashtag/Devil?src=hash" data-query-source="hashtag_click" class="twitter-hashtag pretty-link js-nav" dir="ltr" ><s>#</s><b>Devil</b></a> <a href="/hashtag/anyforty?src=hash" data-query-source="hashtag_click" class="twitter-hashtag pretty-link js-nav" dir="ltr" ><s>#</s><b>anyforty</b></a> <a href="/hashtag/wasted?src=hash" data-query-source="hashtag_click" class="twitter-hashtag pretty-link js-nav" dir="ltr" ><s>#</s><b>wasted</b></a> <a href="/hashtag/king?src=hash" data-query-source="hashtag_click" class="twitter-hashtag pretty-link js-nav" dir="ltr" ><s>#</s><b>king</b></a> <a href="http://somelink" rel="nofollow noopener" dir="ltr" data-expanded-url="http://instagram.com/p/gLi_B9EfOp/" class="twitter-timeline-link" target="_blank" title="http://instagram.com/p/gLi_B9EfOp/" ><span class="tco-ellipsis"></span><span class="invisible">http://</span><span class="js-display-url">instagram.com/p/gLi_B9EfOp/</span><span class="invisible"></span><span class="tco-ellipsis"><span class="invisible">&nbsp;</span></span></a></document>
<document id="396328831285735424" url="https://twitter.com/ReissSudden/status/396328831285735424">@LaurenWeale sorry, that was mean</document>
<document id="396337843909713920" url="https://twitter.com/ReissSudden/status/396337843909713920">@LaurenWeale :(don&#39;t be like that</document>
<document id="396342701568040960" url="https://twitter.com/ReissSudden/status/396342701568040960">@LaurenWeale be like that then <img class="Emoji Emoji--forText" src="https://abs.twimg.com/emoji/v2/72x72/1f624.png" draggable="false" alt="😤" title="Face with look of triumph" aria-label="Emoji: Face with look of triumph"></document>
<document id="396345875360129024" url="https://twitter.com/ReissSudden/status/396345875360129024">Been a pure lazy day today <img class="Emoji Emoji--forText" src="https://abs.twimg.com/emoji/v2/72x72/1f44c.png" draggable="false" alt="👌" title="Ok hand sign" aria-label="Emoji: Ok hand sign"></document>
</documents>
</author>
ありがとうございました!あなたはそれをread
ことができ、@emojiでIMGを交換してからET.fromstring
を使用して、それを解析するデータに応じre.sub
を行うことができますファイルを解析する前に
're.sub(pattern、 '@emoji'、line)'を試したことがありますか?それはループを時代遅れにするでしょう。 – Boldewyn
@Boldewynはちょうどまだ動作していません:( –
'sed'を使わないのはなぜですか? –