作为评估的一部分,我需要使用关键短语加密文件。我正在用python做我的工作,遇到了一个问题。它是使用python 2.7.4编写的
我的代码如下:
导入数组
def encrypter(intext, shift, modder):
plain2 = list(intext)
plain = array.fromlist(plain2)
out = ''
j = 0
key = list(shift)
for c in plain:
if mod > 0:
x = chr((ord(c) + ord(key[(j % (len(plain) - 1)) % len(key)]) - 48) % 58 + 48)
if mod < 0:
x = chr((ord(c) - ord(key[(j % (len(plain) - 1)) % len(key)]) - 48) % 58 + 48)
out += x
j += 1
return out
sel = raw_input("Encrypt (e)/ Decrypt (d)")
if sel == 'e':
mod = 1
intext = open(raw_input("what is your file"),'r')
shift = raw_input("what is your first password")
encrypter(intext, shift, mod)
else:
pass我的问题是,每当我对一个名为text1.txt的文件运行此命令时,我都会收到以下错误:
Traceback (most recent call last):
File "D:/Programming/Computing GCSE/Tasks/task3.py", line 22, in <module>
encrypter(intext, shift, mod)
File "D:/Programming/Computing GCSE/Tasks/task3.py", line 5, in encrypter
plain = array.fromlist(plain2)
AttributeError: 'module' object has no attribute 'fromlist'有人能建议对我的代码进行修改吗?我需要这个相对较快,因为我的评估是在一个小时左右!
发布于 2015-05-07 23:12:38
根本不清楚为什么需要array模块。像这样的东西不会起作用吗?
def encrypter(intext, shift, modder):
plain = intext
out = ''
j = 0
key = shift
for c in plain:
if mod > 0:
x = chr((ord(c) + ord(key[(j % (len(plain) - 1)) % len(key)]) - 48) % 58 + 48)
if mod < 0:
x = chr((ord(c) - ord(key[(j % (len(plain) - 1)) % len(key)]) - 48) % 58 + 48)
out += x
j += 1
return out发布于 2015-05-07 23:15:52
从模块导入数组类
from array import array
plain = array('b', plain2)https://stackoverflow.com/questions/30105064
复制相似问题