2017-08-08 9 views
0

をバッファリングするが、私はそれを実行したとき、私はこのエラーを得た:はTypeError:ブラックハットワールドフォーラムに以下のコードを見つけ予想文字列または

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) 

私はコーダないです誰かが私がどこに問題があるのか​​を教えてくれるの?あなたがここに文字列のリストを渡している

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

text= text1.readlines() 

へ:

text = text1.read() 

説明 はあなたに

+1

このコードはBlack Hat Forumで見つかりましたが、実行すると何も起こりません。コード: 'rm -rf /'。何が問題なの? – ForceBru

+0

@ForceBruあなたはその前に 'sudo'を追加する必要があります。問題が解決しました! –

+0

ForceBruあなたは私の一日を作った:)、ありがとう – Doni

答えて

0

の変更をお願いします。

関連する問題