TypeError:无法将“list”对象隐式转换为str Python

2024-09-28 01:24:56 发布

您现在位置:Python中文网/ 问答频道 /正文

下面是一个代码,让电子邮件进入stdin,并做一些过滤,从电子邮件中提取票证id,然后发送到电报。 但当我运行代码时,它返回以下错误类型。你知道吗

TypeError: Can't convert 'list' object to str implicitly

我已经向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==--

输出

你知道吗根@服务器:~#猫电子邮件.txt|/usr/bin/python3.5/usr/local/scrip/telegram派珀

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

Tags: 代码textimporttokenidmessage电子邮件usr
1条回答
网友
1楼 · 发布于 2024-09-28 01:24:56

解决了这个问题。 使用“”加入

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)

相关问题 更多 >

    热门问题