We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,394 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

How to omit or delete part of a yml file?

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

2
Contributors
5
Replies
11 Hours
Discussion Span
5 Months Ago
Last Updated
7
Views
aaaaaaaaa
Newbie Poster
3 posts since Mar 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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
Gribouillis
Posting Maven
Moderator
3,101 posts since Jul 2008
Reputation Points: 1,130
Solved Threads: 761
Skill Endorsements: 11

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.

aaaaaaaaa
Newbie Poster
3 posts since Mar 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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

Gribouillis
Posting Maven
Moderator
3,101 posts since Jul 2008
Reputation Points: 1,130
Solved Threads: 761
Skill Endorsements: 11

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?

aaaaaaaaa
Newbie Poster
3 posts since Mar 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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".

Gribouillis
Posting Maven
Moderator
3,101 posts since Jul 2008
Reputation Points: 1,130
Solved Threads: 761
Skill Endorsements: 11

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.1269 seconds using 2.69MB