gpt4 book ai didi

redis - 如何将 "\""(双引号)从 lua 输出到控制台?

转载 作者:IT王子 更新时间:2023-10-29 06:07:11 24 4
gpt4 key购买 nike

我正在尝试从 Lua 输出一段联系字符串到控制台。当字符串显示在控制台中时,该字符串的前后自动带有双引号。我想在字符串中间有一些其他的双引号,但我不能那样做。

我已经尝试了几种不同的方法,正如我在下面的评论中所展示的那样,但这些方法都不起作用。输出通常是这样的:

1) "10000\": \"1543412332"
2) "10001\": \"1543233731"
3) "10003\": \"1543637245"
4) "10004\": \"1543227124"
5) "10005\": \"1543226828"

但我希望输出为:

1) "10000": "1543412332"
2) "10001": "1543233731"
3) "10003": "1543637245"
4) "10004": "1543227124"
5) "10005": "1543226828"

这是我的代码

    for index = 1, table.maxn(resultKey) do
local unconcatted = {[1] = resultKey[index], [2] = [[": "]], [3] = resultValue[index]}
-- local unconcatted = {[1] = "\"", [2] = resultKey[index], [3] = "\": \"", [4] = resultValue[index], [5] = "\""}
-- local unconcatted = {[1] = resultKey[index], [2] = "\": \"", [3] = resultValue[index]}
-- local unconcatted = {[1] = resultKey[index], [2] = '\": \"', [3] = resultValue[index]}
local concatted = table.concat(unconcatted);
table.insert(resultFinal, 1, concatted);
end
return resultFinal;

最佳答案

要转义一种引号,请使用另一种!

"'" = single quote literal
'"' = double quote literal
[['"']] = string within [[]] is parsed as is (dropping leading newlines if there are any)
[=[I have [[ and ]] inside!]=] = Use `=` when you need [[]] inside, add more `=` if required

在你的情况下

local unconcatted = {'"', resultKey[index], '": "', resultValue[index], '"'}

应该做的工作。如果您愿意,可以将 ' 对替换为 [[]]

另请注意,我已删除索引。当您只需要一个数组并执行与以前相同的操作时,这是首选的表示法,使您无需对更改进行计数并有可能提高性能。

您的 for 循环不是很好。 table.maxn 在 Lua 5.1 中被弃用,并在以后的版本中被删除。您应该使用新的长度语法:

for index = 1, #resultKey do
end

或者使用ipairs:

for index,key in ipairs(resultKey) do
local unconcatted = {'"', key, '": "', resultValue[index], '"'}

end

关于redis - 如何将 "\""(双引号)从 lua 输出到控制台?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57139095/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com