1,105,197 Community Members

How to omit or delete part of a yml file?

Member Avatar
aaaaaaaaa
Newbie Poster
3 posts since Mar 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

So I have a YML file, and I'm trying to convert it to a different format. I found a YML format converter script and edited it to the best of my ability, and this is what I've come up with:

#!/bin/python
try:
    import yaml
except:
    print "Failed to import module: yaml"

f = open("config.yml")
fh = f.read()
f.close()

orig_dict = yaml.load(fh)

users = orig_dict['users']

space = "    "

print "users:"

for user in users:
    print space + user + ":"
    for category in users[user]:
        if category == "groups":
            print space * 2 + "group:"
            for category_groups in users[user][category]:
                print space * 2 + "- " + category_groups
        else:
            print space * 2 + "ERROR"

It works, but the one problem is that I don't want to include users in the group "default" in the converted file. I've tried several things such as if category_groups != "default": print space * 2 + "- " + category_groups but that only omits the group, not the entire user. Is there a way to either skip users in the default group while printing the file or remove them after the file is printed? Help is appreciated :)

Member Avatar
Gribouillis
Posting Maven
3,452 posts since Jul 2008
Reputation Points: 1,140 [?]
Q&As Helped to Solve: 883 [?]
Skill Endorsements: 18 [?]
Moderator
 
0
 

Add a function to filter users

def _my_users(users):
    for user in users:
        try:
            if not all(cg == "default" for cg in users[user]["group"]):
                yield user
        except KeyError:
            pass

my_users = list(_my_users(users))

for user in my_users:
    # ... etc
Member Avatar
aaaaaaaaa
Newbie Poster
3 posts since Mar 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Where do I put this in the script? I tried putting it at the beginning and the end and I get SyntaxError: invalid syntax when I try to run it. Sorry, I have no experience with Python but all I could find that did something similar to what I needed was a Python script.

Member Avatar
Gribouillis
Posting Maven
3,452 posts since Jul 2008
Reputation Points: 1,140 [?]
Q&As Helped to Solve: 883 [?]
Skill Endorsements: 18 [?]
Moderator
 
0
 

Where is the syntax error ? Are you using a very old version of python ?

Member Avatar
aaaaaaaaa
Newbie Poster
3 posts since Mar 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I'm using 2.7.3 and I tried placing your code before and after the script I have, and I get the same error for both. Today I tried putting it in front of the script again like this:

def _my_users(users):
    for user in users:
        try:
            if not all(cg == "default" for cg in users[user]["group"]):
                yield user
        except KeyError:
            pass

my_users = list(_my_users(users))

for user in my_users:
    print space + user + ":"
    for category in users[user]:
        if category == "groups":
            print space * 2 + "group:"
            for category_groups in users[user][category]:
                print space * 2 + "- " + category_groups
        else:
            print space * 2 + "ERROR"

And it runs with no errors but the output file just says users: with nothing else under it... Is there any way to just say that if a user is in group "default", print their name and the group, and if not just skip it?

Member Avatar
Gribouillis
Posting Maven
3,452 posts since Jul 2008
Reputation Points: 1,140 [?]
Q&As Helped to Solve: 883 [?]
Skill Endorsements: 18 [?]
Moderator
 
0
 

Sorry, I think I misspelled "group" instead of "groups". Can you try again ? What my code does is that it keeps all users for which there is a category_group in users[user]["groups"] which is different from "default".

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article