繁体   English   中英

TypeError:预期的字符串或缓冲区

[英]TypeError: expected string or buffer

在Black Hat World论坛上找到了以下代码,但是当我执行它时,出现了此错误:

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)

我不是编码员,有人可以帮助我确定问题出在哪里吗? 谢谢

更改:

text= text1.readlines()

至:

text = text1.read()

说明:

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

您正在此处传递字符串列表。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM