首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >arnold/用python编写的图书密码

arnold/用python编写的图书密码
EN

Stack Overflow用户
提问于 2017-03-21 12:06:08
回答 2查看 1.7K关注 0票数 4

我正在尝试写一本书的密码解码器,下面是我到目前为止所得到的。

代码语言:javascript
复制
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“。

那是我的代码文件,我正试图把它转换成文字

“段号”、“行号”、“字号”

代码语言:javascript
复制
70 1 3
50 2 2
21 2 9
28 1 6
71 2 2
27 1 4

然后我希望我的输出看起来像这样

代码语言:javascript
复制
word1
word2  
word3
word4 
word5 
word6

顺便说一句,我的书“我需要得到的那个文件”是这样的

word1 word2 word3 word4 word5 word6....word..字..。最后一个词

(单词是,不完全相同,)

相关:How to count paragraphs?

EN

回答 2

Stack Overflow用户

发布于 2017-03-21 12:32:08

您已经知道如何在图书文件中阅读,将其分解为行,并将其中的每一行分解为文字。

如果段落被定义为由"\n\n"分隔,则可以将图书文件的内容放在其中,并将每一段分解为行。或者,在你把书分成几行之后,任何空行都意味着段落的改变。

票数 0
EN

Stack Overflow用户

发布于 2020-10-24 20:06:04

这可能是一个很晚的答案,但现在总比我猜的好吗?

--我完成了一个图书密码实现,我想说的是,;完全按照你的要求做。

  • 它接受一个图书文件(在我的示例测试运行中,在“Shakespeare.txt”底部)
  • 和一条信息(文字)
  • 查找键入的每个单词的索引,并从该->获取相同的单词,但在书中。
  • 它打印出这本书的文字索引。

看一看?希望能帮上忙!

我在这件事上做得很疯狂。花了我几年的时间,完成了这个! 祝你有愉快的一天!

我相信一个工作代码的答案,或者至少是在这个方向上的尝试,这就是为什么我也提供这个代码;真的希望它对你和未来的观众都有帮助!

主要编辑:

编辑1:在这里提供代码,对未来的查看者来说更容易;希望对您也是这样:

主要方案:

起源于我的GitHub: https://github.com/loneicewolf/Book-Cipher-Python

我缩短了它并删除了一些东西(在本例中这是不需要的),以使它更“优雅”(但愿它也变成了)

代码语言:javascript
复制
# 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

(我也在里面加了一个数字,这样它就能证明它也适用于数字,如果将来有人觉得奇怪的话)

它输出您的图书代码:

代码语言:javascript
复制
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

//祝愿威廉。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42926663

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档