Python 2.7 得到一个;索引器:列表索引超出范围“;这对我来说没有意义
我有一个任务要做,就是把盖茨比的故事拿走,去掉所有的标点符号,用“0”s替换“o”。根据故事中的文字,我必须破解一个格式为w1w2w3的PDF,并通过循环不断上升(w2w3w4、w3w4w5、w4w5w6等),直到找到密码。我的问题是我得到了一个索引器:列表索引超出了下一行的范围Python 2.7 得到一个;索引器:列表索引超出范围“;这对我来说没有意义,python-2.7,list,loops,pdf,for-loop,Python 2.7,List,Loops,Pdf,For Loop,我有一个任务要做,就是把盖茨比的故事拿走,去掉所有的标点符号,用“0”s替换“o”。根据故事中的文字,我必须破解一个格式为w1w2w3的PDF,并通过循环不断上升(w2w3w4、w3w4w5、w4w5w6等),直到找到密码。我的问题是我得到了一个索引器:列表索引超出了下一行的范围 password = word_list[list_1] + word_list[list_2] + word_list[list_3] 如果能帮我找到错误的原因,我将不胜感激,因为我在过去的两天里一直在看它,但都没
password = word_list[list_1] + word_list[list_2] + word_list[list_3]
如果能帮我找到错误的原因,我将不胜感激,因为我在过去的两天里一直在看它,但都没有用。如果您注意到我的代码中还有其他错误,请指出它们
import re
import PyPDF2
# Defining the function called "pdf_cracker"
def pdf_cracker():
# Import the shuffle
` from random import shuffle
# Open Gadsby story and define it as the variable "string"
string = open("gadsby.txt", "r").read()
# Uses substitution in the RE module to only include characters from a-z, A-Z and 0-9
new_str = re.sub("[^a-zA-Z0-9]", " ",string)
open("gadsby_no_punctuation.txt", "w").write(new_str)
gadsby_no_punctuation = open("gadsby_no_punctuation.txt", "r").read().split()
word_list = [gadsby_no_punctuation]
list_1 = 0
list_2 = 1
list_3 = 2
for item in word_list:
password = word_list[list_1] + word_list[list_2] + word_list[list_3] # ERROR ON THIS LINE
password = password.replace("o", "0")
password = password.replace("O", "0")
print password
from PyPDF2 import PdfFileReader, PdfFileWriter
pdf_file = ("crack_me.pdf", "rb")
if pdf.isEncrypted == False:
print "The file you're trying to crack doesn't have a password"
if(pdf_file.decrypt(password) == 1):
print "O " + password + " is the password!"
break
else:
print "X " + password + " is not the password"
list_1 = list_1 + 1
list_2 = list_2 + 1
list_3 = list_3 + 1
if __name__ == "__main__":
zip_cracker() # Ignore this line - this is another part of the program, not throwing up any errors.
pdf_cracker()
您正在创建gadsby列表,然后将其放入另一个列表中
gadsby_no_punctuation = "a b c d e f g".split() # A test string
>>> gadsby_no_punctuation
['a', 'b', 'c', 'd', 'e', 'f', 'g']
>>> word_list = [gadsby_no_punctuation]
>>> word_list
[['a', 'b', 'c', 'd', 'e', 'f', 'g']] # a list inside a list
>>> len(word_list)
1 # only 1 item in the list
而不是
word_list = [gadsby_no_punctuation]
使用[:]复制gadsby列表
word_list = gadsby_no_punctuation[:]
只要至少有3个单词,您就不会再出现该错误每次分配5个单词。检查:我想这就是发生的事情吗?在哪里?看看链接是否有用