问题
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
the loop changes the following variables:
- paragraph
- line
- word
and every thing is working just fine .
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..
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 word3 word4 word5...... word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word
word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word
word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word last word
(The words are not identical)
回答1:
You already know how to read in the book file, break it into lines, and break each of those into words.
If paragraphs are defined as being separated by "\n\n"
, you can split
the contents of the book file on that, and break each paragraph into lines. Or, after you break the book into lines, any empty line signals a change of paragraph.
回答2:
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.
- 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 worked as crazy on this one. Took me, literally Years to complete this!
Have a great day!
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!
MAJOR EDIT:
Edit 1: Providing code here, easier for the future viewers; and for you hopefully:
Main Program:
Originally from my GitHub: https://github.com/loneicewolf/Book-Cipher-PythonI shortened it and removed stuff (that wasn't needed in this case) to make it more 'elegant' (& hopefully it became that too)
# 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
TEST ADDED:
EDIT: I think I could provide a sample run with a book, to show it in action, at least.. Sorry for not providing this sooner:
I executed the python script: and I used Shakespeare.txt
as my 'book' file.
input text: King of dragon, lord of gold, queen of time has a secret, which 3 or three, can hold if two of them are dead
(I added a digit in it too, so it would prove that it works with digits too, if somebody in the future wonders)
and it outputs your book code:
27978 130 17479 2974 130 23081 24481 130 726 202 61 64760 278 106853 1204 38417 256 8204 97 6394 130 147 16 17084
For example:
27978
means the 27978'th
word
in Shakespeare.txt
To decrypt it, you feed in the book code and it outputs the plain text! (the words you originally typed in)
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
-> it outputs ->
King of dragon, lord of gold, queen of time has a secret, which 3 or three, can hold if two of them are dead
//Wishes William.
来源:https://stackoverflow.com/questions/42926663/arnold-book-cipher-with-python