小编Jer*_*pel的帖子

在Go中加载动态Yaml结构

我正在尝试将一些Python代码转换为Golang,并且在弄清楚如何加载动态Yaml数据时遇到了一些困难,我认为这是非常基本的。到目前为止,我发现的所有方法都提到了创建Struct和映射值,但这是不可能的,因为每次读取时,我将接收的数据都会有所不同。

这不是真实数据(实际上会从API返回),但以yaml文件为例:

[ ~]$ cat /tmp/example.yaml
Massachusetts:
  cities:
    - name: 'Boston'
      area_code: 617
    - name: 'Springfield'
    - name: 'Worcester'
Virginia:
  cities:
    - name: 'Richmond'
    - name: 'Arlington'
      landmarks:
        - 'The Pentagon'
        - 'National Airport'
        - 'Arlington National Cemetary'
  presidents:
    - 'George Washington'
    - 'Thomas Jefferson'
    - 'James Madison'
    - 'James Monroe'
    - 'William Henry Harrison'
    - 'John Tyler'
Missouri:
  rivers:
    - 'Missouri River'
    - 'Mississippi'
    - 'Arkansas River'
    - 'White River'
Run Code Online (Sandbox Code Playgroud)

在Python中阅读和操作它很简单:

#!/usr/bin/python
import yaml
with open('/tmp/example.yaml', 'r') as fh: …
Run Code Online (Sandbox Code Playgroud)

yaml dynamic go

6
推荐指数
1
解决办法
2383
查看次数

Python ValueError要解压的值太多 - 不是真的吗?

我之前在原因显而易见的地方遇到了这个错误,但我在下面的这个片段中遇到了麻烦.

#!/usr/bin/python

ACL = 'group:troubleshooters:r,user:auto:rx,user:nrpe:r'

for e in ACL.split(','):
  print 'e = "%s"' % e
  print 'type during split = %s' % type(e.split(':'))
  print 'value during split:  %s' % e.split(':')
  print 'number of elements:  %d' % len(e.split(':'))
  for (one, two, three) in e.split(':'):
      print 'one = "%s", two = "%s"' % (one, two)
Run Code Online (Sandbox Code Playgroud)

我已经添加了那些用于调试的print语句,并且已经确认拆分正在生成一个3元素列表,但是当我尝试将它放入3个变量时,我得到:

e = "group:troubleshooters:r"
type during split = <type 'list'>
value during split:  ['group', 'troubleshooters', 'r']
number of elements:  3
Traceback (most recent call last):
  File "/tmp/python_split_test.py", …
Run Code Online (Sandbox Code Playgroud)

python valueerror

2
推荐指数
2
解决办法
368
查看次数

标签 统计

dynamic ×1

go ×1

python ×1

valueerror ×1

yaml ×1