[英]Python replace not replacing everything
I'm trying to replace in my .html file all links with slash at the beginning to ../. 我正在尝试将../开头的所有链接替换为.html文件中的所有链接。 For example: 例如:
<link href="/wp-content/themes/newera/style.css?ver=1" id="stylesheet-css" media="all" rel="stylesheet" type="text/css"/>
to 至
<link href="../wp-content/themes/newera/style.css?ver=1" id="stylesheet-css" media="all" rel="stylesheet" type="text/css"/>
So I've got this code: 所以我有以下代码:
with open(path, 'r') as file :
filedata = str(file.read())
filedata = filedata.replace('href="/', 'href="../')
filedata = filedata.replace('src="/', 'src="../')
with open(path, 'w') as file:
file.write(filedata)
And It replaces but not everything. 而且它取代了一切,但并不能取代一切。 After running function in .html file is this (part of code): 在.html文件中运行函数后,这是代码的一部分:
<link href="../feed/" rel="alternate" title="avium » Feed" type="application/rss+xml"/>
<link href="../comments/feed/" rel="alternate" title="avium » Comments Feed" type="application/rss+xml"/>
<link href="../about/feed/" rel="alternate" title="avium » About Comments Feed" type="application/rss+xml"/>
<link href="../wp-content/uploads/2014/11/favicon.ico" rel="shortcut icon"/>
<link href="../wp-content/plugins/fuse-social-floating-sidebar/inc/css/font-awesome.min.css" rel="stylesheet"/>
<link href="/wp-content/themes/newera/style.css?ver=1" id="stylesheet-css" media="all" rel="stylesheet" type="text/css"/>
<link href="/wp-content/themes/newera/framework/css/jquery.bxslider.css?ver=1" id="oi_bxslider_css-css" media="all" rel="stylesheet" type="text/css"/>
<link href="/wp-content/themes/newera/framework/FlexSlider/flexslider.css?ver=1" id="flex-slider-css" media="all" rel="stylesheet" type="text/css"/>
<link href="/wp-content/themes/newera/framework/css/prettyPhoto.css?ver=1" id="oi_prettyPhoto_css-css" media="all" rel="stylesheet" type="text/css"/>
<link href="/wp-content/plugins/contact-form-7/includes/css/styles.css?ver=4.0.1" id="contact-form-7-css" media="all" rel="stylesheet" type="text/css"/>
<link href="/wp-content/plugins/revslider/rs-plugin/css/settings.css?rev=4.6.0&ver=4.0.18" id="rs-plugin-settings-css" media="all" rel="stylesheet" type="text/css"/>
Any ideas why it's not replacing everything? 有什么想法为什么不能取代所有东西?
EDIT: 编辑:
ok here's full code: https://pastebin.com/n25f0CTh 好的,这里是完整代码: https : //pastebin.com/n25f0CTh
My full functions is: 我的全部功能是:
with open(path, 'r') as file :
filedata = str(file.read())
filedata = filedata.replace('http://'+domain, '')
filedata = filedata.replace('http://www.'+domain, '')
filedata = filedata.replace('www.'+domain, '')
filedata = filedata.replace(domain, '')
filedata = filedata.replace('href="/', 'href="../')
filedata = filedata.replace('src="/', 'src="../')
filedata = filedata.replace('href=""', 'href="../index.html"')
with open(path, 'w') as file:
file.write(filedata)
And variable 'domain' in this exaple is: avium.pl 例如,变量“ domain”是:avium.pl
There seem to be inconsistencies between single and double quotes, for example in 单引号和双引号之间似乎不一致,例如
<link rel="shortcut icon" href="http://avium.pl/wp-content/uploads/2014/11/favicon.ico">
and 和
<link rel='stylesheet' id='oi_bxslider_css-css' href='http://avium.pl/wp-content/themes/newera/framework/css/jquery.bxslider.css?ver=1' type='text/css' media='all' />
Adding 新增中
filedata = filedata.replace("href='/", "href='../")
filedata = filedata.replace("src='/", "src='../")
seems to catch everything 似乎抓住了一切
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.