2013-11-25 16 views
0

打印我有一個文本,例如:如何使單獨的文本文件,使之成爲新的生產線

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

你的格式是什麼?這不是很清楚。 – AHuman

+0

啊..這個問題再次。儘管如此,這段文字已經搞砸了。如果出現這種情況,您必須手動完成。 – aIKid

+0

哈哈@aIKid是這個問題再次。是的,我不知道爲什麼它出現的時候,當我把字典放在文本之前它是完美的 – user2976821

回答

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 
相關問題