代码如下:
#enter code here
source_input = input("enter source url: ")
destination_input = input("enter the destination url: ")
new_source = source_input.replace("https://www.domain.com.au","")
new_destination = destination_input.replace("https://www.domain.com.au","")
symbol = "=>"
symbol_space = len(symbol)
center = symbol.center(symbol_space)
source_final = f" '{new_source}' "
destination_final = f" '{new_destination}' "
print(source_final+center+destination_final)
但此代码不接受多行输入。我想提供多个来源和目的地输入,如:
Actual source input
https://www.domain.com.au/shop
Actual destination input
https://www.domain.com.au/shopwithus
Actual output
'/shop' => '/shopwithus'
desired source inputs to be taken
https://www.domain.com.au/shop
https://www.domain.com.au/main
https://www.domain.com.au/home
https://www.domain.com.au/contactus
desired destiantion inputs to be taken
https://www.domain.com.au/shopwithus
https://www.domain.com.au/gomain
https://www.domain.com.au/homemain
https://www.domain.com.au/contactus
Desired outputs
'/shop' => '/shopwithus'
'/main' => '/gomain'
'/home' => 'homemain'
'/contactus' => '/contactus'
有什么方法可以实现这种输入和输出吗?
这可能是一个横向移动,但您可以将命令行参数与argparse
:一起使用
#!/usr/bin/env python3
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('-s', '--source', help='The source.', required=True, type=str)
parser.add_argument('-d', '--destination', help='The desitnation.', required=True, type=str)
cmd_args = parser.parse_args()
def main() -> None:
print("source :", cmd_args.source)
print("destination:", cmd_args.destination)
if __name__ == "__main__":
main()
然后,当执行时,只需指定如下参数(脚本称为tmp.py
(
python3 tmp.py -s "www.source.com" -d "www.destination.com"
# source : www.source.com
# destination: www.destination.com