sys.argv 在 python 中的用法
sys.argv ussage in python
我只是想知道如何在此特定程序中使用 sys.argv
命令行参数列表而不是输入法。由于argc
在python中不存在,所以长度会被len
方法所阻止,对吧?
提前感谢您的帮助!
MORSE_CODE_DICT = {
'A':'.-',
'B':'-...',
'C':'-.-.',
'D':'-..',
'E':'.',
'F':'..-.',
'G':'--.',
'H':'....',
'I':'..',
'J':'.---',
'K':'-.-',
'L':'.-..',
'M':'--',
'N':'-.',
'O':'---',
'P':'.--.',
'Q':'--.-',
'R':'.-.',
'S':'...',
'T':'-',
'U':'..-',
'V':'...-',
'W':'.--',
'X':'-..-',
'Y':'-.--',
'Z':'--..',
'1':'.----',
'2':'..---',
'3':'...--',
'4':'....-',
'5':'.....',
'6':'-....',
'7':'--...',
'8':'---..',
'9':'----.',
'0':'-----',
}
```python
def encryptor(text):
encrypted_text = ""
for letters in text:
if letters != " ":
encrypted_text = encrypted_text + MORSE_CODE_DICT.get(letters) + " "
else:
encrypted_text += " "
print(encrypted_text)
text_to_encrypt = input("Enter Some Text To Encrypt : ").upper()
encryptor(text_to_encrypt)
sys.argv
的第一个元素是正在执行的程序的名称。剩下的就是传递给程序的参数,由shell管理。例如,文件名扩展 *.txt
将为找到的每个文本文件扩展为一个单独的元素。可以写个测试程序看看不同的展开
test.py:
import sys
print(sys.argv)
运行 的两种方法
$ python test.py hello there buckaroo
['test.py', 'hello', 'there', 'buckaroo']
$ python test.py "hello there buckaroo"
['test.py', 'hello there buckaroo']
一个简单的解决方案是将参数连接起来,这样一个人就可以输入带引号或不带引号的输入
import sys
text_to_encrypt = " ".join(sys.argv[1:]).upper()
encryptor(text_to_encrypt)
添加我们得到的代码
$ python morsecoder.py hello there buckaroo
.... . .-.. .-.. --- - .... . .-. . -... ..- -.-. -.- .- .-. --- ---
注意我们不需要特别知道 argv 的长度。 Python 喜欢迭代 - 通常情况下,如果你需要某物的长度,那你就做错了。
sys.argv
= 传递给 Python 脚本的命令行参数列表。 argv[0]
是脚本名称。
你可以这样试试,也可以使用strip()
方法删除leading/trailing和字符(space是默认要删除的前导字符)
def encrypt():
return "".join([MORSE_CODE_DICT.get(letter,"NA") for word in sys.argv[1:] for letter in word])
我只是想知道如何在此特定程序中使用 sys.argv
命令行参数列表而不是输入法。由于argc
在python中不存在,所以长度会被len
方法所阻止,对吧?
提前感谢您的帮助!
MORSE_CODE_DICT = {
'A':'.-',
'B':'-...',
'C':'-.-.',
'D':'-..',
'E':'.',
'F':'..-.',
'G':'--.',
'H':'....',
'I':'..',
'J':'.---',
'K':'-.-',
'L':'.-..',
'M':'--',
'N':'-.',
'O':'---',
'P':'.--.',
'Q':'--.-',
'R':'.-.',
'S':'...',
'T':'-',
'U':'..-',
'V':'...-',
'W':'.--',
'X':'-..-',
'Y':'-.--',
'Z':'--..',
'1':'.----',
'2':'..---',
'3':'...--',
'4':'....-',
'5':'.....',
'6':'-....',
'7':'--...',
'8':'---..',
'9':'----.',
'0':'-----',
}
```python
def encryptor(text):
encrypted_text = ""
for letters in text:
if letters != " ":
encrypted_text = encrypted_text + MORSE_CODE_DICT.get(letters) + " "
else:
encrypted_text += " "
print(encrypted_text)
text_to_encrypt = input("Enter Some Text To Encrypt : ").upper()
encryptor(text_to_encrypt)
sys.argv
的第一个元素是正在执行的程序的名称。剩下的就是传递给程序的参数,由shell管理。例如,文件名扩展 *.txt
将为找到的每个文本文件扩展为一个单独的元素。可以写个测试程序看看不同的展开
test.py:
import sys
print(sys.argv)
运行 的两种方法
$ python test.py hello there buckaroo
['test.py', 'hello', 'there', 'buckaroo']
$ python test.py "hello there buckaroo"
['test.py', 'hello there buckaroo']
一个简单的解决方案是将参数连接起来,这样一个人就可以输入带引号或不带引号的输入
import sys
text_to_encrypt = " ".join(sys.argv[1:]).upper()
encryptor(text_to_encrypt)
添加我们得到的代码
$ python morsecoder.py hello there buckaroo
.... . .-.. .-.. --- - .... . .-. . -... ..- -.-. -.- .- .-. --- ---
注意我们不需要特别知道 argv 的长度。 Python 喜欢迭代 - 通常情况下,如果你需要某物的长度,那你就做错了。
sys.argv
= 传递给 Python 脚本的命令行参数列表。 argv[0]
是脚本名称。
你可以这样试试,也可以使用strip()
方法删除leading/trailing和字符(space是默认要删除的前导字符)
def encrypt():
return "".join([MORSE_CODE_DICT.get(letter,"NA") for word in sys.argv[1:] for letter in word])