arnold/使用python的书密码 [英] arnold/book cipher with python
问题描述
我正在尝试编写一本书的密码解码器,以下是我到目前为止所学到的内容.
I'm trying to write a book cipher decoder, and the following is what i got so far.
code = open("code.txt", "r").read()
my_book = open("book.txt", "r").read()
book = my_book.txt
code_line = 0
while code_line < 6 :
sl = code.split('\n')[code_line]+'\n'
paragraph_num = sl.split(' ')[0]
line_num = sl.split(' ')[1]
word_num = sl.split(' ')[2]
x = x+1
循环会更改以下变量:
- 段落
- 行
- 单词
一切正常.
但是我现在需要的是如何指定段落然后是行然后是单词while循环中的for循环会完美地工作..
but what I need now is how to specify the paragraph then the line then the word a for loop in the while loop would work perfectly..
所以我想从段落编号"paragraph_num"中获取和行号"line_num"单词号"word_num"
so I want to get from paragraph number "paragraph_num" and line number "line_num" the word number "word_num"
那是我的代码文件,我正在尝试将其转换为单词
that's my code file, which I'm trying to convert into words
段落号",行号",单词号"
"paragraph number","line number","word number"
70 1 3
50 2 2
21 2 9
28 1 6
71 2 2
27 1 4
然后我希望我的输出看起来像这样
and then I want my output to look something like this
word1
word2
word3
word4
word5
word6
顺便说一句,我的书我需要从中获取单词的那个文件"看起来像这样
by the way , my book "that file that i need to get the words from" looks something like this
word1 word2 word3word4 word5 word6 ......单词..单词.. 最后一个单词
(单词不相同)
相关:如何计算段落?
推荐答案
这可能是一个很晚的答案.但现在比从来没有想过要好吗?
This may be quite a late answer; but better now than never I guess?
我已经完成了图书密码的实现,我想说的是完全符合您的要求.
I completed a book cipher implementation, that I would like to say; does exactly what you are asking after.
- 需要一个图书文件(在我的示例测试运行中,位于底部"Shakespeare.txt")
- 和一条消息(单词)
- 查找输入的每个单词的索引,并从中获取相同的单词->但在书中.
- 它会打印出这本书的单词索引.
- It takes a book file (in my example test run, at the bottom "Shakespeare.txt")
- and a message (words)
- finds the index of each words typed in, and gets the same words from that -> but in the book.
- It prints out the book's-Words-Indexes.
看看吗?希望对您有帮助!
Give it a look? Hope it helps!
我为此疯狂.送给我,从字面上看的年份这个!
祝你有美好的一天!
我相信使用有效代码的答案,或者至少是朝这个方向尝试的方法.这就是为什么我也提供此代码;真希望它能对您和您双方有所帮助未来的观众!
I believe a answer with working code, or at least a try in that direction That's why I'm providing this code too; Really hope it helps both you & The future viewers!
主要
在此处提供代码,方便以后的观看者使用;希望对您有帮助:
Edit 1: Providing code here, easier for the future viewers; and for you hopefully:
我将其缩短并删除了一些东西(在这种情况下并不需要),以使其更加优雅"(并且希望它也变得如此)
# Replace "document1.txt" with whatever your book / document's name is.
BOOK="document1.txt" # This contains your "Word Word Word Word ...." I believed from the very start that you meant, they are not the same - (obviously)
# Read book into "boktxt"
def GetBookContent(BOOK):
ReadBook = open(BOOK, "r")
txtContent_splitted = ReadBook.read();
ReadBook.close()
Words=txtContent_splitted
return(txtContent_splitted.split())
boktxt = GetBookContent(BOOK)
words=input("input text: ").split()
print("\nyou entered these words:\n",words)
i=0
words_len=len(words)
for word in boktxt:
while i < words_len:
print(boktxt.index(words[i]))
i=i+1
x=0
klist=input("input key-sequence sep. With spaces: ").split()
for keys in klist:
print(boktxt[int(klist[x])])
x=x+1
已添加测试:
我想我可以提供一本随书一起运行的示例,至少以实际的方式展示它.很抱歉没有尽快提供:我执行了python脚本:我将 Shakespeare.txt
用作我的'book'文件.
输入文字:龙之王,金之王,时光之女王有一个秘密,如果其中两个已死,则可以保留3或3个秘密
(我也在其中添加了一个数字,因此如果将来有人想知道的话,它也将证明它也适用于数字)
并输出您的图书代码:
27978 130 17479 2974 130 23081 24481 130 726 202 61 64760 278 106853 1204 38417 256 8204 97 6394 130 147 16 17084
例如:
27978
表示 Shakespeare.txt
要解密它,您需要输入书代码并输出纯文本!(您最初输入的字词)
To decrypt it, you feed in the book code and it outputs the plain text! (the words you originally typed in)
输入键序列sep.带空格: 27978 130 17479 2974 130 23081 24481 130 726 202 61 64760 278 106853 1204 38417 256 8204 97 6394 130 147 16 17084
input key-sequence sep. With spaces: 27978 130 17479 2974 130 23081 24481 130 726 202 61 64760 278 106853 1204 38417 256 8204 97 6394 130 147 16 17084
->它输出->
龙之王,金之王,时光之女王有一个秘密,如果其中两个已经死了,这个秘密可以保留3到3个
//希望威廉.
这篇关于arnold/使用python的书密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!