如何在Python中将字符串或字典拆分成多行?

4
我已经制作了一个字典:
{
    "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!"
        ]
    }
}

我希望它返回一个字符串,所以我在它后面加上了str()。 但我不知道如何像换行一样将其分开。 我需要的是:

----------
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']
----------

我应该将它转换为字符串然后再分割吗?还是从字典中分割并将其作为字符串处理? 顺便说一句,我使用的是Python 3。

顺便说一下,我通过很多代码做了这个字典,所以直接添加空格或 \n 很困难。 - Polsop
1个回答

3

试试这个:

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

输出结果:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接