我有一个简单的 Telegram Bot,带有 python-telegram-bot 包,我使用 pandas 导入 csv:
df = pd.read_csv('data.csv')
键 | 文字 |
---|---|
00 | 文字1 文本2 文本3 |
. | 一些 文字 |
. | 其他 文字 |
99 | 文本4 文本5 文本6 |
然后我在用户消息的数据框中进行搜索:
answer = df['text'].str.contains(update.message.text, case=False)]
机器人向用户发送回复消息:
await update.message.reply_text(answer)
但输出显示“ ” 标签:
text1 \n text2 \n text3
我想显示文字:
text1
text2
text3
我正在为这个问题苦苦挣扎。在使用 dataframe 之前,我使用 TinyDb,一切正常。 我该如何解决?
谢谢
我尝试将列的数据类型更改为字符串,将 csv 导出到列表,文件的编码。
我尝试了你的情况,但它对我有用。
import pandas as pd
# I am using json format for my case
data = [
{"key": "01", "text": "text1 \n text2 \n text3"},
{"key": "01", "text": "some \n text"},
{"key": "02", "text": "other \n text"},
{"key": "99", "text": "text4 \n text5 \n text6"},
]
df = pd.DataFrame(data)
# ...
# answer = df[df["text"].str.contains("some", case=False)]
answer = df[df["text"].str.contains(update.message.text, case=False)]
if not answer.empty:
print(answer.values[0][1].encode()) # check raw text
# out: b'some \n text'
print(type(answer.values[0][1])) # check type
# out: <class 'str'>
await update.message.reply_text(answer.values[0][1])