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

问题描述 投票:0回答:1

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

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

我已将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)

电子邮件内容

--===============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==--

输出

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

1735
1735
Traceback (most recent call last):
  File "/usr/local/scripts/telegram-piper.py", line 22, in <module>
    telegram_bot_sendtext(tikid)
  File "/usr/local/scripts/telegram-piper.py", line 9, in telegram_bot_sendtext
    send_text = 'https://api.telegram.org/bot' + bot_token + '/sendMessage?chat_id=' + bot_chatID + '&parse_mode=Markdown&text=' + bot_message
TypeError: Can't convert 'list' object to str implicitly```
python python-3.x
1个回答
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)
© www.soinside.com 2019 - 2024. All rights reserved.