4

I did make a dictionary :

{
    "PerezHilton": {
        "name": "Perez Hilton",
        "following": [
            "tomCruise",
            "katieH",
            "NicoleKidman"
        ],
        "location": "Hollywood, California",
        "web": "http://www.PerezH...",
        "bio": [
            "Perez Hilton is the creator and writer of one of the most famous websites",
            "in the world. And he also loves music - a lot!"
        ]
    },
    "tomCruise": {
        "name": "Tom Cruise",
        "following": [
            "katieH",
            "NicoleKidman"
        ],
        "location": "Los Angeles, CA",
        "web": "http://www.tomcruise.com",
        "bio": [
            "Official TomCruise.com crew tweets. We love you guys!",
            "Visit us at Facebook!"
        ]
    }
}

I want it to return a string so i add str() to it. But I have no idea how to break it like lines. what I required is :

----------
PerezHilton
name: Perez Hilton
location: Hollywood, California
website: http://www.PerezH... 
bio:
Perez Hilton is the creator and writer of one of the most famous websites
in the world. And he also loves music - a lot!
following: ['tomCruise', 'katieH', 'NicoleKidman']
----------
tomCruise
name: Tom Cruise
location: Los Angeles, CA
website: http://www.tomcruise.com
bio:
Official TomCruise.com crew tweets. We love you guys!
Visit us at Facebook!
following: ['katieH', 'NicoleKidman']
----------

should I change it to string and then break it? or break them from dict and take it as a string? btw, I used python 3

1
  • btw I did the dictionary through many codes, so it's hard to add space or \n directly. Commented Dec 7, 2015 at 2:56

1 Answer 1

3

try this:

_dict = {
    "PerezHilton": {
        "name": "Perez Hilton",
        "following": [
            "tomCruise",
            "katieH",
            "NicoleKidman"
        ],
        "location": "Hollywood, California",
        "web": "http://www.PerezH...",
        "bio": [
            "Perez Hilton is the creator and writer of one of the most famous websites",
            "in the world. And he also loves music - a lot!"
        ]
    },
    "tomCruise": {
        "name": "Tom Cruise",
        "following": [
            "katieH",
            "NicoleKidman"
        ],
        "location": "Los Angeles, CA",
        "web": "http://www.tomcruise.com",
        "bio": [
            "Official TomCruise.com crew tweets. We love you guys!",
            "Visit us at Facebook!"
        ]
    }
}    

def str_generator(key, value):
    if isinstance(value, str):
        return key +': ' + value
    elif key == 'bio':
        return key + ':\n' + '\n'.join(value)
    else:
        return key + ': ' + str(value)


a = ""


for key, value in _dict.items():
    a += ('----------\n' + key + '\n')
    for _key, _value in value.items():
        a += (''.join(str_generator(_key, _value)) + '\n')


print(a)

the output:

----------
tomCruise
location: Los Angeles, CA
following: ['katieH', 'NicoleKidman']
name: Tom Cruise
bio:
Official TomCruise.com crew tweets. We love you guys!
Visit us at Facebook!
web: http://www.tomcruise.com
----------
PerezHilton
location: Hollywood, California
following: ['tomCruise', 'katieH', 'NicoleKidman']
name: Perez Hilton
bio:
Perez Hilton is the creator and writer of one of the most famous websites
in the world. And he also loves music - a lot!
web: http://www.PerezH...
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.