Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将字符串转换为python 3中的有效wordpress url以供请求库使用_Python_Wordpress_Python 3.x_Url_Python Requests - Fatal编程技术网

将字符串转换为python 3中的有效wordpress url以供请求库使用

将字符串转换为python 3中的有效wordpress url以供请求库使用,python,wordpress,python-3.x,url,python-requests,Python,Wordpress,Python 3.x,Url,Python Requests,给定一个字符串https://websiteurl/path/photo的url.jpeg 如何将其转换为合法urlhttps://websiteurl/path/photos-url.jpeg从Wordpress的角度使用python3.5。这个url将通过发送带有json的post请求来使用,其中src将是上述合法url的键和值 上传照片时urlhttps://websiteurl/path/photos-url.jpeg已提交给它。('已删除,空间转换为-) 我看到的唯一方法是使用”htt

给定一个字符串
https://websiteurl/path/photo的url.jpeg

如何将其转换为合法url
https://websiteurl/path/photos-url.jpeg
从Wordpress的角度使用python3.5。这个url将通过发送带有json的post请求来使用,其中src将是上述合法url的键和值

上传照片时url
https://websiteurl/path/photos-url.jpeg
已提交给它。('已删除,空间转换为-)

我看到的唯一方法是使用
”https://websiteurl/path/photo“的url.jpeg.replace(\”,“”)。replace(“,“-”)


有什么通用的pythonic方法吗?

您可以使用re.sub和str.replace。例如:

import re

special_chars = ["?", "[", "]", "/", "\\", "=", "<", ">", ":", ";", ",", "'", "\"", "&", "$", "#", "*", "(", ")", "|", "~", "`", "!", "{", "}", "%", "+"]
uri = "photo's url.jpeg"

#use str.replace
for i in special_chars:
    uri = uri.replace(i, "")

#or re.sub
#uri = re.sub("\?|\[|\]|/|\\|\=|<|>|:|;|,|'|\"|\&|\$|#|\*|\(|\)|~|`|!|\{|\}|%|\+", "", uri)

uri = re.sub("\s+", "-", uri)
print(uri)
重新导入
特殊字符=[“?”、“[”、“]”、“/”、“\\”、“=”、“:”、“;”、“、”、“、”、“$”、“*”、“(“、”)、“|”、“~”、“、”、“!”、“{”、“}、“%、”、“+”]
uri=“照片的url.jpeg”
#使用str.replace
对于特殊角色的我:
uri=uri.replace(i,“”)
#或者说
#uri=re.sub(“\?\[\ \ \]\/\ \ \ \ \=\ \ \ \ \ \ \ \ \ \ \ \:\;\,\'\”,“\ \$\ \ \ \ \ \ \ \ \ \*\(\)\!\ \ \ \ \ \ \”,“,”,uri)
uri=re.sub(“\s+”,“-”,uri)
打印(uri)
这将把
照片的url.jpeg
更改为
照片的url.jpeg

看看wordpress是如何在php中实现的:

可能使用
re.sub