我正在尝试使用美妙的python-telegram-bot模块创建一个简单的 Telegram Bot ,但我无法将字典与默认"{0:<20} {1}".format(key, value)想法对齐。
让我给你举个例子:
MAP = {
"one": "1",
"two": "2",
"three": "3",
"four": "4",
"five": "5",
"six": "6",
"seven": "7",
"eight": "8"
}
tmpstring = ""
for key, value in MAP.items():
tmpstring = tmpstring + "{0:<20} {1}".format(key, value) + "\n"
print(tmpstring)
context.bot.send_message(chat_id=update.message.chat_id, text=tmpstring)
Run Code Online (Sandbox Code Playgroud)
印刷完成看起来像这样:
one 1
two 2
three 3
four 4
five 5
six 6
seven 7
eight 8
Run Code Online (Sandbox Code Playgroud)
正如预期的那样完美对齐,但 Telegram 中的消息如下所示:
所以我的问题是:如何对齐聊天消息,使其看起来像打印输出?
所以我有一个 SteamApp 对象列表:
>>> games
[<SteamApp "Counter-Strike: Global Offensive" (730)>, <SteamApp "XCOM: Enemy Unknown" (200510)>]
Run Code Online (Sandbox Code Playgroud)
我也有这段代码:
ownscs = []
if '"Counter-Strike: Global Offensive"' in games:
print('Owns CS')
ownscs.append(foo)
Run Code Online (Sandbox Code Playgroud)
我想做的就是检查是否有人拥有《反恐精英》,但问题的关键来了,列表游戏不可迭代,并且没有属性查找,如果我尝试这两个游戏,我会得到:
if any('"Counter-strike: Global Offensive"' in s for s in games):
TypeError: argument of type 'SteamApp' is not iterable
Run Code Online (Sandbox Code Playgroud)
和
if games.find('"Counter-strike: Global Offensive"') != -1:
AttributeError: 'SteamApp' object has no attribute 'find'
Run Code Online (Sandbox Code Playgroud)
所以我的问题是:当《反恐精英:全球攻势》显然既不可迭代也不可找到时,我如何检查它的游戏列表。
如果您想知道那是什么,我使用https://github.com/smiley/steamapi创建 SteamApp 对象。
我有一个字段叫Status我source.FieldGetText("Status")在我的查询 Querysave.
此状态可以是
现在我的问题是Instr:
If Instr("two",Status) > 0 Then...
Run Code Online (Sandbox Code Playgroud)
每个状态的回报是:
为什么它不像案例2那样为案例3和4返回1?据我了解它Instr检查子串是否在String中并返回它的位置,对于Case 3和4,它应该是1?
仅供Python参考,我会这样做:
if "test" in "this is a test":
print("String contains test")
Run Code Online (Sandbox Code Playgroud)