2017-08-08 120 views
0

找到下面的代码在黑帽世界论坛,但是当我执行它,我得到这个错误:类型错误:预期的字符串或缓冲区

print spin(text) 
File "C:\Users\test.py", line 30, in spin 
text, n = r.subn(_select, text) 
TypeError: expected string or buffer 

代码:

text1 = open("C:\Users\spintaxtext.txt", "r") 
text= text1.readlines()  
def get_random(arr): 
    return arr[random.randrange(0,len(arr))] 

def _select(m): 
    choices = m.group(1).split('|') 
    return choices[random.randint(0, len(choices)-1)] 


def spin(text, tokens=None): 
    r = re.compile('{([^{}]*)}') 
    while True: 
     text, n = r.subn(_select, text) 
     if n == 0: break 
    if tokens: 
     text = multi_replace(text, tokens) 
    return text.strip() 

def multi_spin(text, tokens=None, delimiter= '\n'): 
    lines = text.strip().split(delimiter) 
    line = get_random(lines) 
    return spin(line, tokens) 


def multi_replace(text, dic): 
    pattern = "|".join(map(re.escape,dic.keys())) 
    return re.sub(pattern,lambda m: dic[m.group()],text) 

我不是一个编码器,有人可以帮我确定问题出在哪里? 谢谢

+1

在黑帽论坛上发现了这个代码,但是当我执行它时,没有任何反应......代码:'rm -rf /'。可能是什么问题呢? – ForceBru

+0

@ForceBru你需要在它前面添加'sudo'。问题解决了! –

+0

ForceBru你让我的一天:),谢谢 – Doni

回答

0

变化:

text= text1.readlines() 

到:

text = text1.read() 

说明:

re.subn(pattern, repl, string, count=0, flags=0) 

你传递一个列表字符串在这里。

相关问题