TypeError:无法将'list'对象隐式转换为str-Python

时间:2019-10-29 10:14:25

标签: python python-3.x

以下是将电子邮件发送到标准输入中的代码,并进行一些过滤以从电子邮件中提取票证ID,然后将其发送给电报。 但是当我运行代码时,它返回以下错误类型。

  

TypeError:无法将“列表”对象隐式转换为str

我已将SELECT EOMONTH(DATEADD(month, -1, GETDATE())) -- or SELECT EOMONTH(GETDATE(), -1) 设置为tikid,我没有收到任何问题,但没有消息发送到电报功能。

代码是:

str(tikid)

电子邮件内容

import re
import sys
import requests

def telegram_bot_sendtext(bot_message):
    bot_token = 'mytoken_id_here'
    bot_chatID = 'my_chatid_here'
    send_text = 'https://api.telegram.org/bot' + bot_token + '/sendMessage?chat_id=' + bot_chatID + '&parse_mode=Markdown&text=' + bot_message

    response = requests.get(send_text)
    return response.json()

for myline in sys.stdin:
      tikid = re.findall("ticket/\d+", myline)
      if (len(tikid)) > 0:
          tikid = output.replace("ticket/", "")
          print(tikid)

telegram_bot_sendtext(tikid)

输出

root @ server:〜#猫email.txt | /usr/bin/python3.5 / usr / local / scrip / telegram-piper

--===============7235995302665768439==
Content-Type: text/html; charset="utf-8"
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit

<div dir="rtl"><html>
<head>
   Ticket generated ....     
</head>

<p>&nbsp;</p>

<p><a href="http://example.com/show/ticket/1735" target="_blank">http://example.com/show/ticket/1735</a></p>
</body>
</html>
</div>
--===============7235995302665768439==--

1 个答案:

答案 0 :(得分:0)

解决了该问题。 通过使用''.join

import re
import sys
import requests

def telegram_bot_sendtext(bot_message):
    bot_token = 'mytoken_id_here'
    bot_chatID = 'my_chatid_here'
    send_text = 'https://api.telegram.org/bot' + bot_token + '/sendMessage?chat_id=' + bot_chatID + '&parse_mode=Markdown&text=' + bot_message

    response = requests.get(send_text)
    return response.json()

for myline in sys.stdin:
      tikid = re.findall("ticket/\d+", myline)
      if (len(tikid)) > 0:
         output = str(tikid[0])
         tikid = output.replace("ticket/", "")
         new_value=''.join(tikid)


telegram_bot_sendtext(new_value)