如何在文件中制作单独的文本并使其以新行打印

时间:2013-11-25 03:31:00

标签: python dictionary python-3.3

我的文字如下:

count, 93mosts, your avglen, 4.419354838709677shorts, ['i', 'a']longs, ['stretched']

如何让它看起来像这样?

shorts:in,me,of,us 
longs:calibrate,fantastic
mosts:even,more,words
count:anIntVal
avglen:aFloatVal

ab 5
and 8
...
yours 2

这是我将字典放入文件的方法:

def write_report(r, filename):
with open(filename, "w") as f:
    for k, v in r.items():
        f.write('{}, {}'.format(k, v) )
f.close()
return f

原始字典:

{'shorts': ['i', 'a'], 'mosts': 'your', 'avglen': 4.419354838709677, 'count': 93, 'longs': ['stretched'], 'freqs': {'yet': 1, 'but': 2, 'of': 4, 'most': 1, 'that': 1, 'number': 1, 'less': 1, 'neer': 1, 'than': 1, 'deserts': 1, 'lies': 1, 'so': 1, 'song': 1, 'all': 1, 'write': 1, 'your': 6, 'shows': 1, 'come': 2, 'hides': 1, 'beauty': 1, 'half': 1, 'not': 1, 'earthly': 1, 'alive': 1, 'child': 1, 'touched': 1, 'papers': 1, 'graces': 1, 'poet': 1, 'knows': 1, 'some': 1, 'to': 2, 'touches': 1, 'old': 1, 'yellowed': 1, 'eyes': 1, 'say': 1, 'and': 5, 'time': 2, 'tongue': 1, 'life': 1, 'could': 1, 'fresh': 1, 'age': 2, 'filled': 1, 'were': 2, 'rhyme': 1, 'men': 1, 'will': 1, 'rage': 1, 'true': 1, 'believe': 1, 'metre': 1, 'termed': 1, 'you': 1, 'stretched': 1, 'high': 1, 'my': 3, 'is': 1, 'like': 1, 'an': 1, 'it': 3, 'heavenly': 1, 'though': 1, 'antique': 1, 'rights': 1, 'who': 1, 'the': 2, 'verse': 1, 'if': 2, 'as': 1, 'in': 4, 'should': 2, 'with': 2, 'i': 1, 'parts': 1, 'live': 1, 'a': 2, 'such': 1, 'truth': 1, 'heaven': 1, 'tomb': 1, 'be': 2, 'their': 1, 'which': 1, 'scorned': 1, 'would': 1, 'faces': 1, 'poets': 1, 'numbers': 1, 'yours': 1, 'twice': 1, 'this': 1}}

1 个答案:

答案 0 :(得分:0)

您需要使用json转换原始字典以便写入文件:

>>> d = {'shorts': ['i', 'a'], 'mosts': 'your', 'avglen': 4.419354838709677, 'count': 93, 'longs': ['stretched'], 'freqs': {'yet': 1, 'but': 2, 'of': 4, 'most': 1, 'that': 1, 'number': 1, 'less': 1, 'neer': 1, 'than': 1, 'deserts': 1, 'lies': 1, 'so': 1, 'song': 1, 'all': 1, 'write': 1, 'your': 6, 'shows': 1, 'come': 2, 'hides': 1, 'beauty': 1, 'half': 1, 'not': 1, 'earthly': 1, 'alive': 1, 'child': 1, 'touched': 1, 'papers': 1, 'graces': 1, 'poet': 1, 'knows': 1, 'some': 1, 'to': 2, 'touches': 1, 'old': 1, 'yellowed': 1, 'eyes': 1, 'say': 1, 'and': 5, 'time': 2, 'tongue': 1, 'life': 1, 'could': 1, 'fresh': 1, 'age': 2, 'filled': 1, 'were': 2, 'rhyme': 1, 'men': 1, 'will': 1, 'rage': 1, 'true': 1, 'believe': 1, 'metre': 1, 'termed': 1, 'you': 1, 'stretched': 1, 'high': 1, 'my': 3, 'is': 1, 'like': 1, 'an': 1, 'it': 3, 'heavenly': 1, 'though': 1, 'antique': 1, 'rights': 1, 'who': 1, 'the': 2, 'verse': 1, 'if': 2, 'as': 1, 'in': 4, 'should': 2, 'with': 2, 'i': 1, 'parts': 1, 'live': 1, 'a': 2, 'such': 1, 'truth': 1, 'heaven': 1, 'tomb': 1, 'be': 2, 'their': 1, 'which': 1, 'scorned': 1, 'would': 1, 'faces': 1, 'poets': 1, 'numbers': 1, 'yours': 1, 'twice': 1, 'this': 1}}
>>> import json
>>> converted_obj = json.dumps(d)
>>> converted_obj
'{"count": 93, "shorts": ["i", "a"], "longs": ["stretched"], "mosts": "your", "freqs": {"all": 1, "less": 1, "yet": 1, "touches": 1, "deserts": 1, "termed": 1, "with": 2, "should": 2, "to": 2, "verse": 1, "touched": 1, "song": 1, "rhyme": 1, "half": 1, "not": 1, "heaven": 1, "like": 1, "rights": 1, "twice": 1, "truth": 1, "graces": 1, "antique": 1, "old": 1, "some": 1, "alive": 1, "heavenly": 1, "filled": 1, "shows": 1, "be": 2, "poets": 1, "men": 1, "yellowed": 1, "come": 2, "of": 4, "could": 1, "metre": 1, "faces": 1, "tomb": 1, "number": 1, "lies": 1, "stretched": 1, "your": 6, "would": 1, "their": 1, "live": 1, "poet": 1, "eyes": 1, "knows": 1, "that": 1, "but": 2, "child": 1, "believe": 1, "true": 1, "than": 1, "rage": 1, "this": 1, "as": 1, "will": 1, "were": 2, "my": 3, "and": 5, "is": 1, "it": 3, "neer": 1, "an": 1, "high": 1, "say": 1, "numbers": 1, "hides": 1, "in": 4, "if": 2, "write": 1, "parts": 1, "which": 1, "papers": 1, "you": 1, "beauty": 1, "though": 1, "who": 1, "most": 1, "scorned": 1, "earthly": 1, "such": 1, "tongue": 1, "a": 2, "i": 1, "age": 2, "life": 1, "so": 1, "time": 2, "fresh": 1, "the": 2, "yours": 1}, "avglen": 4.419354838709677}'
>>> loaded_obj = json.loads(converted_obj)
>>> loaded_obj == d
True