在Python中如何存储相同参数的两个不同变量?

huangapple go评论38阅读模式
英文:

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:

http://example.com

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

huangapple
  • 本文由 发表于 2023年3月31日 23:14:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/75900125.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定