#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    10
    Rep Power
    0

    'module' object has no attribute 'safe_load'


    I am not sure why I am getting the error in the subject line.

    pydoc yaml shows that safe_load is a function:

    safe_load(stream)
    Parse the first YAML document in a stream
    and produce the corresponding Python object.
    Resolve only basic YAML tags.

    [root@server python]# python yaml.py
    Traceback (most recent call last):
    File "yaml.py", line 3, in <module>
    import yaml
    File "/home/user/python/yaml.py", line 10, in <module>
    dataMap = yaml.safe_load(f)
    AttributeError: 'module' object has no attribute 'safe_load'
    [root@server python]# cat yaml.py
    Code:
    #!/usr/bin/python
    
    import yaml
    #import yaml.loader.Loader
    #from yaml import load, dump
    
    f = open('/etc/puppet/hieradata/file.yaml')
    # use safe_load instead load
    #dataMap = yaml(f)
    dataMap = yaml.safe_load(f)
    f.close()
    
    #pretty print
    print dataMap
    
    open('file.yaml', "w")
    yaml.dump(dataMap, f)
    f.close()
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    114
    Rep Power
    3
    Rename your script. It's importing itself rather than the yaml module you want.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    10
    Rep Power
    0

    error on yaml.dump line


    I renamed and it runs now. Thanks for the tip.

    I need to find a way to write these to the file one on each line without the single quotes and the colon.

    example output.
    hostname1 192.168.100.11
    hostname2 192.168.100.12



    It is erroring on the dump now. I had to comment out this line
    #yaml.dump(dataMap, f)

    current output with the above line commented:
    python /home/user/python/hiera.py
    {'hostname1': '192.168.100.11', 'hostname2': '192.168.100.12', 'entries': None}

    Error
    Code:
    Traceback (most recent call last):
      File "/home/user/python/hiera.py", line 17, in <module>
        yaml.dump(dataMap, f)
      File "/usr/lib64/python2.6/site-packages/yaml/__init__.py", line 202, in dump
        return dump_all([data], stream, Dumper=Dumper, **kwds)
      File "/usr/lib64/python2.6/site-packages/yaml/__init__.py", line 190, in dump_all
        dumper.represent(data)
      File "/usr/lib64/python2.6/site-packages/yaml/representer.py", line 29, in represent
        self.serialize(node)
      File "/usr/lib64/python2.6/site-packages/yaml/serializer.py", line 54, in serialize
        self.serialize_node(node, None, None)
      File "/usr/lib64/python2.6/site-packages/yaml/serializer.py", line 107, in serialize_node
        self.serialize_node(key, node, None)
      File "/usr/lib64/python2.6/site-packages/yaml/serializer.py", line 90, in serialize_node
        style=node.style))
      File "/usr/lib64/python2.6/site-packages/yaml/emitter.py", line 115, in emit
        self.state()
      File "/usr/lib64/python2.6/site-packages/yaml/emitter.py", line 229, in expect_document_root
        self.expect_node(root=True)
      File "/usr/lib64/python2.6/site-packages/yaml/emitter.py", line 255, in expect_node
        self.expect_flow_mapping()
      File "/usr/lib64/python2.6/site-packages/yaml/emitter.py", line 312, in expect_flow_mapping
        self.write_indicator(u'{', True, whitespace=True)
      File "/usr/lib64/python2.6/site-packages/yaml/emitter.py", line 814, in write_indicator
        self.stream.write(data)
    ValueError: I/O operation on closed file
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,843
    Rep Power
    480
    Use re or simply

    mystring.replace("'",'').replace(':','')

    First you'll need to have a string to work with.
    io.StringIO could help---I'm asleep.
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    114
    Rep Power
    3
    That one's pretty simple: you forgot an "f = " before your second call to open.

IMN logo majestic logo threadwatch logo seochat tools logo