Sherlock Holmes argparse 模块任务
Sherlock Holmes argparse module task
我被分配了一项任务,似乎超过了我目前 python 的能力。尽管如此,我确实想解决它,但不知道从哪里开始。特别是这里的这一点:“编写一个程序,将文件作为参数,读取它,对其进行解码,然后打印解码后的文本”。非常感谢任何帮助!
任务描述:
莫里亚蒂教授又来捣乱了!玛丽设法得到了一份包含他计划的一部分的文件,但它是经过编码的。她还没来得及破译,莫里亚蒂就挟持了她,华生医生前去营救。
Holmes 预料到了这一点,并在 Whosebug 上找到了一个简单的凯撒密码解码器来自己处理它:
def decode_Caesar_cipher(s, n):
alpha = " ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz',.?!"
s = s.strip()
text = ''
for c in s:
text += alpha[(alpha.index(c) + n) % len(alpha)]
print('Decoded text: "' + text + '"')
但是,福尔摩斯不会处理文件,所以你必须帮助他!您可以在下方找到 Mary 从 Moriarty 的笔记本电脑中获得的文件。下载它并使用 Holmes 找到的代码编写一个程序,将文件作为参数,读取它,对其进行解码,然后打印解码后的文本。为此,请使用您的 IDE。得到答案后,将其复制并粘贴到答案字段中。
如果您的参数 --file
存储在变量 args
中,您可以通过这种方式读取传递给脚本的文件:
filename = args.file
opened_file = open(filename)
encoded_text = opened_file.read() # read the file into a string
opened_file.close() # always close the files you've opened
提示1:最初福尔摩斯从玛丽那里得到的文件名为“13.txt”,因此他预设这可能是偏移量n
。检查这个理论,但请记住,解码偏移量必须带负号。
提示 2:设置命令行以快速循环许多关键/'n' 参数。找到解码后的消息后,将其粘贴到不带引号的答案字段中。
提示3:你不需要使用argsparser,主要问题是找到“n”。
数据集可以从this link
下载
您需要的是 argparse
Python 标准库。它可以在您的情况下按如下方式使用:
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--file", required=True, help="Path to file")
args = parser.parse_args()
然后在你的例子中main
函数您可以按如下方式访问参数:
file = args.file
您可以这样调用程序:
python your_program.py --file filename/or/path/to/file
在 Python 中,我建议 运行 该程序与文件所在的目录(文件夹)相同。
现在你在变量中有了文件的 name 并且可以按你喜欢的方式加载它。
编辑:
所以最后,您的程序结构可能如下所示:
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--file", required=True, help="Path to file")
args = parser.parse_args()
def read_file(filename):
opened_file = open(filename)
encoded_text = opened_file.read() # read the file into a string
opened_file.close() # always close the files you've opened
return encoded_text
def decode_Caesar_cipher(s, n):
alpha = " ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz',.?!"
s = s.strip()
text = ''
for c in s:
text += alpha[(alpha.index(c) + n) % len(alpha)]
print('Decoded text: "' + text + '"')
def main():
filename = args.file
encoded_text = read_file(filename)
# Note that before calling decode_Caesar_cipher, you need to determine 'n',
# which seems to be the main task for this programming challenge.
# This can be achieved with e.g. a for loop
for n in range(1, 30):
print("n =", n, end=", ")
decode_Caesar_cipher(encoded_text, n)
print("n = -", n, sep="", end=", ")
decode_Caesar_cipher(encoded_text, -n)
if __name__=="__main__":
main()
我被分配了一项任务,似乎超过了我目前 python 的能力。尽管如此,我确实想解决它,但不知道从哪里开始。特别是这里的这一点:“编写一个程序,将文件作为参数,读取它,对其进行解码,然后打印解码后的文本”。非常感谢任何帮助!
任务描述:
莫里亚蒂教授又来捣乱了!玛丽设法得到了一份包含他计划的一部分的文件,但它是经过编码的。她还没来得及破译,莫里亚蒂就挟持了她,华生医生前去营救。
Holmes 预料到了这一点,并在 Whosebug 上找到了一个简单的凯撒密码解码器来自己处理它:
def decode_Caesar_cipher(s, n):
alpha = " ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz',.?!"
s = s.strip()
text = ''
for c in s:
text += alpha[(alpha.index(c) + n) % len(alpha)]
print('Decoded text: "' + text + '"')
但是,福尔摩斯不会处理文件,所以你必须帮助他!您可以在下方找到 Mary 从 Moriarty 的笔记本电脑中获得的文件。下载它并使用 Holmes 找到的代码编写一个程序,将文件作为参数,读取它,对其进行解码,然后打印解码后的文本。为此,请使用您的 IDE。得到答案后,将其复制并粘贴到答案字段中。
如果您的参数 --file
存储在变量 args
中,您可以通过这种方式读取传递给脚本的文件:
filename = args.file
opened_file = open(filename)
encoded_text = opened_file.read() # read the file into a string
opened_file.close() # always close the files you've opened
提示1:最初福尔摩斯从玛丽那里得到的文件名为“13.txt”,因此他预设这可能是偏移量n
。检查这个理论,但请记住,解码偏移量必须带负号。
提示 2:设置命令行以快速循环许多关键/'n' 参数。找到解码后的消息后,将其粘贴到不带引号的答案字段中。
提示3:你不需要使用argsparser,主要问题是找到“n”。
数据集可以从this link
下载您需要的是 argparse
Python 标准库。它可以在您的情况下按如下方式使用:
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--file", required=True, help="Path to file")
args = parser.parse_args()
然后在你的例子中main
函数您可以按如下方式访问参数:
file = args.file
您可以这样调用程序:
python your_program.py --file filename/or/path/to/file
在 Python 中,我建议 运行 该程序与文件所在的目录(文件夹)相同。 现在你在变量中有了文件的 name 并且可以按你喜欢的方式加载它。
编辑: 所以最后,您的程序结构可能如下所示:
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--file", required=True, help="Path to file")
args = parser.parse_args()
def read_file(filename):
opened_file = open(filename)
encoded_text = opened_file.read() # read the file into a string
opened_file.close() # always close the files you've opened
return encoded_text
def decode_Caesar_cipher(s, n):
alpha = " ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz',.?!"
s = s.strip()
text = ''
for c in s:
text += alpha[(alpha.index(c) + n) % len(alpha)]
print('Decoded text: "' + text + '"')
def main():
filename = args.file
encoded_text = read_file(filename)
# Note that before calling decode_Caesar_cipher, you need to determine 'n',
# which seems to be the main task for this programming challenge.
# This can be achieved with e.g. a for loop
for n in range(1, 30):
print("n =", n, end=", ")
decode_Caesar_cipher(encoded_text, n)
print("n = -", n, sep="", end=", ")
decode_Caesar_cipher(encoded_text, -n)
if __name__=="__main__":
main()