首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在Python 3中逐行替换正则表达式

在Python 3中逐行替换正则表达式
EN

Stack Overflow用户
提问于 2018-08-06 02:29:25
回答 1查看 68关注 0票数 0

我已经编写了一些代码来尝试执行以下操作

  • 打开SVG文件我之前已经在Python代码中检索到了
  • 在文件中查找特定的正则表达式找到匹配的,用特定字符串替换文本,例如https://regex101.com/r/IECsHu/1
  • Then从匹配的url中检索JPG (请参见上面的指向regex101.com)

的链接

但是,这不起作用,并完全清除文件(因此它是0字节)

我想我肯定已经很接近让它工作了,但还没有成功。如有任何指导,不胜感激。

pagenumber=1
directory_in_str='/home/somewhere/somedir/'
pathlist = Path(directory_in_str).glob('**/*.svg')
for path in pathlist:
#because path is object not string
  path_in_str = str(path)   
  print(path_in_str)

  with open(path_in_str, 'r+') as f:
    for line in f:
        myregex = r"(?:xlink:href\=\")(.*)(?:\?q=80\"\/\>)"
        result = myregex.search(line)

        if result:
            #If a match is found, replace the text in the line          
            origfullimgfilename = result
            formattedpagenumber = '{:0>3}'.format(pagenumber)
            replfullimgfilename='page-'+str(formattedpagenumber)+'-img1.jpg'
            line = re.sub(origfullimgfilename, replfullimgfilename, line.rstrip())  
            #Then retrieve the file! (origfullimgfilename)
            try:
                urllib.request.urlretrieve(origfullimgfilename+"?q=100", replfullimgfilename)
            except urllib.error.HTTPError as e:
                print("HTTP Error: "+str(e.code)+" - SVG URL: "+str(origfullimgfilename)+" - aborting\n")
                break
pagenumber += 1
EN

回答 1

Stack Overflow用户

发布于 2018-08-06 04:10:04

lines = """<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<!-- Page 1 -->
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0" y="0" width="1198" height="1576" viewBox="0 0 1198 1576" version="1.1" style="display: block;margin-left: auto;margin-right: auto;">
<path d="M0,0 L0,1576 L1198,1576 L1198,0 Z " fill="#FFFFFF" stroke="none"/>
<image preserveAspectRatio="none" x="0" y="0" width="1199" height="1577" xlink:href="https://cdn-assets.somewhere.com/f929e7b4404d3e48918cdc0ecd4efbc9fa91dab5_2734/9c237c7e35efe88878f9e5f7a3555f7a379ed9ee9d95b491b6d0003fd80afc6b/9c68a28d6b5161f7e975a163ff093a81172916e23c778068c6bfefcfab195154.jpg?q=80"/>
<g fill="#112449">
<use xlink:href="#f0_2" transform="matrix(19.1,0,0,19.1,885.3,204.3)"/>
<use xlink:href="#f0_o" transform="matrix(19.1,0,0,19.1,910,204.3)"/>
<use xlink:href="#f0_t" transform="matrix(19.1,0,0,19.1,930.3,204.3)"/>
<use xlink:href="#f0_f" transform="matrix(19.1,0,0,19.1,949.6,204.3)"/>"""

import re

newlines = []
for line in lines.split('\n'):
    myregex = re.compile(r"(?:xlink:href\=\")(.*)(?:\?q=80\"\/\>)")
    result = myregex.search(line)

    if result:
        print(result)
        #If a match is found, replace the text in the line          
        origfullimgfilename = result.group(1)
        replfullimgfilename='page-##-img1.jpg'
        line = re.sub(origfullimgfilename, replfullimgfilename, line)  

    newlines.append(line)

print( '\n'.join(newlines) )
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51697171

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档