2016-09-22 13 views
1

fortuneコマンドの出力をフェッチしてWebベースのWhatsAppに送信しようとしています。 fortuneコマンドから出力を取得できますが、Wh​​atsAppに送信すると、fortune出力は別の行/メッセージとして出力されます。どうすればそれらを1つにして、それを1つのメッセージとして送信できますか?ありがとう。Pythonでターミナル出力文字列を1つの文字列に結合します

fortune_list = ["(?i)art","(?i)comp","(?i)cookie","(?i)drugs","(?i)education","(?i)ethnic","(?i)food"] 

for i in range(len(fortune_list)): 
    if re.search(re.compile(fortune_list[i]),reply): 
     cmd = ['fortune', fortune_list_reply[i]] 
     output = subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0] 
     print output 
     input_box[1].send_keys(output) 
     time.sleep(1) 
     b.find_element_by_class_name('send-container').click() 

端末(print output

enter image description here

別々のメッセージとしてのWhatsApp上で送信ouputを上に出力されます。

enter image description here

単一メッセージとして

所望の出力。

enter image description here

編集1:reprを使用して:文字列を合体させるが、これらの文字を使用しました。 regex replaceを使って文字を置き換えることはできません。

"XXXI:\n\tThe optimum committee has no members.\nXXXII:\n\tHiring consultants to conduct studies can be an excellent means of\n\tturning problems into gold -- your problems into their gold.\nXXXIII:\n\tFools rush in where incumbents fear to tread.\nXXXIV:\n\tThe process of competitively selecting contractors to perform work\n\tis based on a system of rewards and penalties, all distributed\n\trandomly.\nXXXV:\n\tThe weaker the data available upon which to base one's conclusion,\n\tthe greater the precision which should be quoted in order to give\n\tthe data authenticity.\n\t\t-- Norman Augustine\n"

編集2:回答が追加されました。

+0

うん、うまくいかないようです。事実、私はreplaceを使ってすべての長さの空白を1つに減らそうとしました。置換は役に立たないようです。 –

+0

これは機能しません。私は 'repr'を試してみました。それは\ n、\ t、そしてすべての文字列を1つの文字列として作成しました。その後、正規表現を使って\ n \ tを置き換えようとしましたが、動作しません。 –

+0

うん。私は更新しました。 –

答えて

0

端末の出力文字列を1つにまとめるには、出力を新しい行に分割してリストに格納します。

sentence = "" 
cmd = ['fortune', fortune_list_reply[i]] 
output = subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0] 
output = output.split('\n') 
for string in output: 
    string = string.split("\t")          
    for substring in string: 
     if len(substring) == 0: 
      continue 
     else: 
      sentence = sentence + " " + substring 
関連する問題