英文:
How to store two separate variables of the same argument in Python?
问题
website = "http://example.com"
websitefilename = re.sub(r'[^\w\s-]', '-', website).strip().lower()
websitefilename = re.sub(r'[-\s]+', '-', website)
print(website)
print(websitefilename)
英文:
I am trying to write a function in Python which takes in a website name and simply returns two versions of it in two separate variables:
The first variable website should look like the original argument with no changes: http://example.com
The second variable websitefilename should look like this: http-example-com
I have attempted to store these in two separate variables like so:
def websitefile(website):
websitefilename = re.sub(r'[^\w\s-]', '-', website).strip().lower()
websitefilename = re.sub(r'[-\s]+', '-', website)
print(website)
print(websitefilename)
websitefile(http://example.com)
But both website and websitefilename- return the same thing:
How do you make website return http://example.com and websitefilename return http-example-com?
I need them differently because Windows for some reason can't have slashes in filenames.
答案1
得分: 1
在代码的第3行,应该使用websitefilename
而不是website
。
以下是应该有效的代码:
import re
def websitefile(website):
websitefilename = re.sub(r'[^\w\s-]', '-', website).strip().lower()
websitefilename = re.sub(r'[-\s]+', '-', websitefilename)
print(website)
print(websitefilename)
websitefile("http://example.com")
英文:
In line 3 of your code you should use websitefilename
instead of website
Below code should work
import re
def websitefile(website):
websitefilename = re.sub(r'[^\w\s-]', '-', website).strip().lower()
websitefilename = re.sub(r'[-\s]+', '-', websitefilename)
print(website)
print(websitefilename)
websitefile("http://example.com")
答案2
得分: 1
import re
def websitefile(website):
# 第一个正则表达式
websitefilename = re.sub(r'[^\w\s-]', '-', website).strip().lower()
# 第二个表达式是在 website 上操作,而不是预期的 websitefilename 上操作。
websitefilename = re.sub(r'[-\s]+', '-', websitefilename)
# 返回一个包含原始版本和清理版本的元组
return website, websitefilename
# 运行函数,将两个字符串收集为一个元组
res_tup = websitefile("http://example.com")
print(f"{res_tup=}")
print(f"{res_tup[0]=}")
print(f"{res_tup[1]=}\n")
# 重新运行函数,将两个字符串收集为单独的变量
website, filename = websitefile("http://example.com")
print(f"{website=}")
print(f"{filename=}")
输出:
res_tup=('http://example.com', 'http-example-com')
res_tup[0]='http://example.com'
res_tup[1]='http-example-com'
website='http://example.com'
filename='http-example-com'
英文:
import re
def websitefile(website):
# Your first regex expression
websitefilename = re.sub(r'[^\w\s-]', '-', website).strip().lower()
# Your second expression was operating on website, not websitefilename as intended.
websitefilename = re.sub(r'[-\s]+', '-', websitefilename)
# Return a tuple containing the original version and the santitized version
return website, websitefilename
# Run the function, collecting the two strings as a tuple
res_tup = websitefile("http://example.com")
print(f"{res_tup=}")
print(f"{res_tup[0]=}")
print(f"{res_tup[1]=}\n")
# Re-run the function, collecting the two strings as separate variables
website, filename = websitefile("http://example.com")
print(f"{website=}")
print(f"{filename=}")
Output:
res_tup=('http://example.com', 'http-example-com')
res_tup[0]='http://example.com'
res_tup[1]='http-example-com'
website='http://example.com'
filename='http-example-com'
答案3
得分: 0
第二个正则表达式替换是不正确的。应该是
websitefilename = re.sub(r'[-\s]+', '-', websitefilename)
这样你的输出是:
http://example.com
http-example-com
英文:
your second regex replace is incorrect. It should be
websitefilename = re.sub(r'[-\s]+', '-', websitefilename)
with that your output is :
http://example.com
http-example-com
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论