python - 如何将 linux 命令行参数添加到 python 代码?
问题描述
我对 Python 很陌生,正在尝试创建一种受 ROT13 启发的加密方法。
我有点完成了代码,但我想在 Linux 终端中使用它,并带有诸如-e
- 之类的参数(我想-e
代表“加密”,-h
这显然代表帮助。)
我做了一些谷歌搜索,发现了有关导入“argparse”的信息,我尝试了这样的事情:
import argparse
parser =argparse.ArgumentParser(description='An
encryption method inspired from Rot13.')
args = parser.parse_args()
它没有用。
任何阅读材料或代码修复都会非常有帮助。
这是我的python代码:
import argparse
parser = argparse.ArgumentParser(description='An encryption method inspired from Rot13.')
args = parser.parse_args()
import sys
# Dictionary to lookup the index of alphabets
alphabet = {'A' : 1, 'B' : 2, 'C' : 3, 'D' : 4, 'E' : 5,
'F' : 6, 'G' : 7, 'H' : 8, 'I' : 9, 'J' : 10,
'K' : 11, 'L' : 12, 'M' : 13, 'N' : 14, 'O' : 15,
'P' : 16, 'Q' : 17, 'R' : 18, 'S' : 19, 'T' : 20,
'U' : 21, 'V' : 22, 'W' : 23, 'X' : 24, 'Y' : 25, 'Z' : 26}
# Dictionary to lookup alphabets corresponding to the index after inc
enchabet = {0 : 'Z', 1 : 'A', 2 : 'B', 3 : 'C', 4 : 'D', 5 : 'E',
6 : 'F', 7 : 'G', 8 : 'H', 9 : 'I', 10 : 'J',
11 : 'K', 12 : 'L', 13 : 'M', 14 : 'N', 15 : 'O',
16 : 'P', 17 : 'Q', 18 : 'R', 19 : 'S', 20 : 'T',
21 : 'U', 22 : 'V', 23 : 'W', 24 : 'X', 25 : 'Y'}
# Function to rotLPWen the string
# according to the inc provided
def rotLPWen(msg, inc):
calc = ''
for char in msg:
# checking for space
if(char != ' '):
# looks up the dictionary and
# adds the inc to the index
num = ( alphabet[char] + inc ) % 26
# looks up the second dictionary for
# the inced alphabets and adds them
calc += enchabet[num]
else:
# adds space
calc += ' '
#print("You can only type one word at a time!")
#sys.exit(1)
return calc
# Function to rotLPWde the string
# according to the inc provided
def rotLPWde(msg, inc):
calced = ''
for char in msg:
# checks for space
if(char != ' '):
# looks up the dictionary and
# subtracts the inc to the index
num = ( alphabet[char] - inc + 26) % 26
# looks up the second dictionary for the
# inced alphabets and adds them
calced += enchabet[num]
else:
# adds space
calced += ' '
#print("You can only type one word at a time!")
#sys.exit(1)
return calced
# driver function to run the program
def main():
# use 'upper()' function to convert any lowercase characters to uppercase
msg = input("Plase type something!")
msg = msg.strip()
if ' ' in msg.strip():
words = msg.split(' ')
encrypted = ''
increments = []
for word in words:
# Create an increment for the number of characters in the sentence
increment = len(word)
inc = increment
result = rotLPWen(word.upper(), inc)
encrypted += result + ' '
increments.append(inc)
else:
increments = len(msg)
inc = increments
encrypted = rotLPWen(msg.upper(), inc)
#Creates a list from the sentence
#list1 = list(map(len,msg.split()))
print(encrypted)
print(increments)
# Executes the main function
if __name__ == '__main__':
main()
解决方案
推荐阅读
- amazon-web-services - AWS Fargate 定价任务
- java - 尝试合并arraylist时不断收到IndexOutOfBounds异常
- python - 关于python离散卷积的问题
- javascript - Google Sheets - UrlFetchapp.fetch 在调试器中工作的自定义函数,但从工作表调用时不
- linux - 期待/发送问题
- php - MySQL 返回值未格式化 (PHP)
- python - 简单的python函数问题我想不通
- excel - 我有一个 vba 代码可以在单个单元格中返回多个匹配项,但是执行需要很长时间。任何加速宏的方法
- json - Swift json动态键解析json
- java - 在嵌套的 Try/Catch 中将异常传递给更高的 Try/Catch [需要嵌套]