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 :)

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

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.

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

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?

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".

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.