我正在尝试写一本书的密码解码器,下面是我到目前为止所得到的。
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循环中的单词a for循环将完美地工作。
因此,我想从段落号"paragraph_num“和行号"line_num”中得到单词"word_num“。
那是我的代码文件,我正试图把它转换成文字
“段号”、“行号”、“字号”
70 1 3
50 2 2
21 2 9
28 1 6
71 2 2
27 1 4然后我希望我的输出看起来像这样
word1
word2
word3
word4
word5
word6顺便说一句,我的书“我需要得到的那个文件”是这样的
word1 word2 word3 word4 word5 word6....word..字..。最后一个词
(单词是,不完全相同,)
发布于 2017-03-21 12:32:08
您已经知道如何在图书文件中阅读,将其分解为行,并将其中的每一行分解为文字。
如果段落被定义为由"\n\n"分隔,则可以将图书文件的内容放在其中,并将每一段分解为行。或者,在你把书分成几行之后,任何空行都意味着段落的改变。
发布于 2020-10-24 20:06:04
这可能是一个很晚的答案,但现在总比我猜的好吗?
--我完成了一个图书密码实现,我想说的是,;完全按照你的要求做。
看一看?希望能帮上忙!
我在这件事上做得很疯狂。花了我几年的时间,完成了这个! 祝你有愉快的一天!
我相信一个工作代码的答案,或者至少是在这个方向上的尝试,这就是为什么我也提供这个代码;真的希望它对你和未来的观众都有帮助!
主要编辑:
编辑1:在这里提供代码,对未来的查看者来说更容易;希望对您也是这样:
主要方案:
起源于我的GitHub: https://github.com/loneicewolf/Book-Cipher-Python
我缩短了它并删除了一些东西(在本例中这是不需要的),以使它更“优雅”(但愿它也变成了)
# 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' file.
输入文本:King of dragon, lord of gold, queen of time has a secret, which 3 or three, can hold if two of them are dead
(我也在里面加了一个数字,这样它就能证明它也适用于数字,如果将来有人觉得奇怪的话)
它输出您的图书代码:
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的意思是27978'th word in Shakespeare.txt
要解密它,您需要输入图书代码,然后它输出纯文本!(你最初输入的单词)
输入键序列九月。带空格:27978 130 17479 2974 130 23081 24481 130 726 202 61 64760 278 106853 1204 38417 256 8204 97 6394 130 147 16 17084
-> it输出->
King of dragon, lord of gold, queen of time has a secret, which 3 or three, can hold if two of them are dead
//祝愿威廉。
https://stackoverflow.com/questions/42926663
复制相似问题