想要拆分字符串
Want to split a string
我正在使用 discord.py 为名为 rocket league 的游戏制作 discord 机器人。在火箭联盟中,你可以交易物品,而我的交易 discord 就是用于此类交易的。所以我决定我希望能够记录所有交易,列出交易不和谐 ID 的人,以及他们玩火箭联盟的平台(因为没有跨平台交易)。
交易报价如下所示:
[H] 项 [W] 报价
我想拆分该字符串,以便我可以将 [H] 和 [W] 放在 .xlsx 文件的不同列中 (excel)
如果不清楚,只需要求澄清不清楚的地方。
谢谢!
你可以这样做
text = "[H] items [W] offer"
items = text.split("items")[0]
offer = text.split("items")[1].split("offer")[0]
正则表达式可以解决您的问题:
import re
regex = r"(?P<items>\d+) items (?P<offers>\d+) offer"
test_str = "10 items 30 offer"
variables = re.match(regex, test_str).groupdict()
# {'items': '10', 'offers': '30'}
variables.get('items')
# '10'
variables.get('offers')
# '30'
我正在使用 discord.py 为名为 rocket league 的游戏制作 discord 机器人。在火箭联盟中,你可以交易物品,而我的交易 discord 就是用于此类交易的。所以我决定我希望能够记录所有交易,列出交易不和谐 ID 的人,以及他们玩火箭联盟的平台(因为没有跨平台交易)。 交易报价如下所示:
[H] 项 [W] 报价
我想拆分该字符串,以便我可以将 [H] 和 [W] 放在 .xlsx 文件的不同列中 (excel) 如果不清楚,只需要求澄清不清楚的地方。 谢谢!
你可以这样做
text = "[H] items [W] offer"
items = text.split("items")[0]
offer = text.split("items")[1].split("offer")[0]
正则表达式可以解决您的问题:
import re
regex = r"(?P<items>\d+) items (?P<offers>\d+) offer"
test_str = "10 items 30 offer"
variables = re.match(regex, test_str).groupdict()
# {'items': '10', 'offers': '30'}
variables.get('items')
# '10'
variables.get('offers')
# '30'