[英]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.