首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在两个字符串之间更改int

在两个字符串之间更改int
EN

Stack Overflow用户
提问于 2021-04-22 23:54:34
回答 1查看 38关注 0票数 0

我需要在两个字符串或类似的字符串之间替换int。例:

代码语言:javascript
复制
["revolter"] = {
    ["name"] = "Revolter",
    aaaa = 195000, #This value (195000) needs to be tripled
    ["category"] = "sports",
    ["shop"] = "pdm",
},
["blade"] = {
    ["name"] = "Blade",
    aaaa = 19000,#This value (19000) needs to be tripled
    ["category"] = "muscle",
    ["shop"] = "pdm",
},

我的代码自动取款机:

代码语言:javascript
复制
import re

f = open("fail.txt","r")
faili_sisu = f.read()
f.close()
new_value = #
faili_sisu = re.sub(r"(.*?aaaa = ).*?(,.*)", r"\1replaceble_int\2", faili_sisu)
faili_sisu = faili_sisu.replace("replaceble_int", str(new_value))

f = open("fail.txt", "w")
f.write(faili_sisu)
f.close()

我想知道如何使int/str介于"aaaa =“和”to变量“之间

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-04-23 00:06:48

re.sub()中的替换可以是一个函数。它可以将匹配的字符串转换为整数并乘以它。

代码语言:javascript
复制
faili_sisu = re.sub(r'(?<=aaaa = )\d+', lambda m: str(3 * int(m.group(0))), faili_sisu)

演示

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67222213

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档