我有这个简单的以下程序:
package main
import (
"fmt"
yaml "gopkg.in/yaml.v2"
)
type Test struct {
SomeStringWithQuotes string `yaml:"someStringWithQuotes"`
SomeString string `yaml:"someString"`
SomeHexValue string `yaml:"someHexValue"`
}
func main() {
t := Test{
SomeStringWithQuotes: "\"Hello World\"",
SomeString: "Hello World",
SomeHexValue: "0xDef9C64256DeE61ebf5B212238df11C7E532e3B7",
}
yamlBytes, _ := yaml.Marshal(t)
fmt.Print(string(yamlBytes))
}
这将打印以下内容,并明显表明 Go 决定何时引用字符串:
someStringWithQuotes: '"Hello World"'
someString: Hello World
someHexValue: 0xDef9C64256DeE61ebf5B212238df11C7E532e3B7
但是,当我尝试使用以下 Python 脚本读取此 YAML 时:
import yaml
yaml_str = """
someStringWithQuotes: '"Hello World"'
someString: Hello World
someHexValue: 0xDef9C64256DeE61ebf5B212238df11C7E532e3B7
"""
print(yaml.load(yaml_str))
它将十六进制值解析为整数。如果我现在使用以下代码序列化回 YAML:
import yaml
import sys
yaml_str = """
someStringWithQuotes: '"Hello World"'
someString: Hello World
someHexValue: 0xDef9C64256DeE61ebf5B212238df11C7E532e3B7
"""
print(yaml.dump(yaml.load(yaml_str)))
我得到:
someHexValue: 1272966107484048169783147972546098614451903325111
someString: Hello World
someStringWithQuotes: '"Hello World"'
我怎样才能最好地确保保留十六进制格式?不幸的是,我个人对 Go 端的代码没有任何影响(但对于尝试做类似事情的其他人来说,Go 端解决方案仍然受欢迎)。
慕田峪4524236
相关分类