fortune
コマンドの出力をフェッチしてWebベースのWhatsAppに送信しようとしています。 fortune
コマンドから出力を取得できますが、WhatsAppに送信すると、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
)
別々のメッセージとしてのWhatsApp上で送信ouputを上に出力されます。
単一メッセージとして所望の出力。
編集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:回答が追加されました。
うん、うまくいかないようです。事実、私はreplaceを使ってすべての長さの空白を1つに減らそうとしました。置換は役に立たないようです。 –
これは機能しません。私は 'repr'を試してみました。それは\ n、\ t、そしてすべての文字列を1つの文字列として作成しました。その後、正規表現を使って\ n \ tを置き換えようとしましたが、動作しません。 –
うん。私は更新しました。 –