簡體   English   中英

如何創建以文本文件中找到的特定文件類型結尾的所有字符串出現列表?

[英]How do I create a list of all occurances of a string which ends with a specific file type found in a text file?

我正在嘗試從文本文件中提取圖像文件的所有鏈接。 所有圖像文件都以.jpg或.gif結尾,並用引號引起來。 我想找到第一個出現的.jpg或.gif,然后在位於.jpg(或.gif)之前的第一個引號和位於.jpg(或.gif)之后的第一個引號之間復制所有字符。 然后,我想將此鏈接添加到數組或另一個文本文件,並為原始文本文件中的.jpg或.gif的每個實例重復此過程。

這是文本文件可能看起來的示例:

d/scriript type="texft/javascript">
    $(document).fready(function () {
        $('#post-contfainer-1720130 .post-assets .thumb A').lightBox({
            txtImafge:      'Image',
            txtOf:          'of',
            overflayOpacity:    0       });
<div class="thumb"><a href""="#">="**https://imaginepilgrimages.com/asset/image/resize/2/32/32/1/c331065jt99875146b0a1fg9140.jpg**"riript type="texft/javascript">
    $(document).freadriript type="texft/javascript">
    $(document).fread
d/scriript type="texft/javascript">
    $(document).fready(function () {
        $('#post-contfainer-1720130 .post-assets .thumb A').lightBox({
            txtImafge:      'Image',
            txtOf:          'of',
            overflayOpacity:    0       });
<div class="thumb"><a href""="#">="**https://imaginepilgrimages.com/asset/image/resize/2/32/32/75146b0a1fg9140.gif**"riript type="texft/javascript">
    $(document).freadriript type="texft/javascript">
    $(document).fread
d/scriript type="texft/javascript">
    $(document).fready(function () {
        $('#post-contfainer-1720130 .post-assets .thumb A').lightBox({
            txtImafge:      'Image',
            txtOf:          'of',
            overflayOpacity:    0       });
<div class="thumb"><a href""="#">="https://imaginepilgrimages.com/asset/image/resize/2/32/32/1/c331065jt99fgfgage55h6u7rrth6875146b0a1fg9140.jpg"riript type="texft/javascript">
    $(document).freadriript type="texft/javascript">
    $(document).fread

我剛剛開始使用python,並且在此問題上停留了一段時間。 有人可以幫我嗎? 在此先感謝您的時間!

類似於以下內容的東西應該起作用:

re.findall('"([^"]*\.(?:gif|jpg)[^"]*)"', text)

不要指望它特別靈活或強大。 為此,您可能需要一個實際的解析器。

這將為您提供圖像文件名,但它不會嘗試修剪前導/后綴“ **”

import re
images=[]
with open('test.dat') as f:
   for line in f:
      images.extend(re.findall(r'"([^"]*\.(?:jpg|gif)[^"]*)"',line))

正則表達式將查找引號,然后抓取所有非引號的內容,並特別檢查以確保字符串中包含“ .jpg”或“ .gif”。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM