无法以适当的格式输出txt

时间:2015-06-03 16:36:28

标签: python dictionary subset subset-sum

这是我的代码。问题是输出看起来像这样

2015-06-03 19:32:11.225085 {'2015-01-21-20:56:45.mp3': 1}{'negative': -2}{'2015-01-15-21:28:23.mp3': 1}

我希望像字典一样......就像下面的这个一样,所以我可以把它作为字典读回来,从第一个子集中删除键并继续输出第二个等等,直到没有其他子集总和存在...

2015-06-03 19:32:11.225085 {'2015-01-21-20:56:45.mp3': 1, 'negative': -2, '2015-01-15-21:28:23.mp3': 1}

有什么想法吗?

先进的

import os, sys,re,gzip, pickle 
from itertools import combinations
import json
from datetime import datetime
mp3folder = raw_input('Please copy paste the mp3s path:')
lowerin = input('Please enter your total playlist time in NEGATIVE seconds and hit ENTER:')
r = {}
drk = os.listdir(mp3folder) 
drifiles = list(drk)
r = dict.fromkeys(drifiles, 0)
for key in r.keys():

        print ('Please enter the duration of...')
        print(key)
        r[key] = input('in seconds and hit ENTER:')

r['negative'] = lowerin
d = {}
neg = 0
pos = 0
dates = datetime.now()
dates = str(dates)
f = open("dict.txt",'ab')
f.write('\n'+dates+'\n')
f.close()
for (w,v) in r.iteritems():
        if v > 0: pos += v
        else:     neg += v

sums = [0] * (pos - neg + 1)

for (w,v) in r.iteritems():
        s = sums[:]
        if not s[v - neg]: s[v - neg] = (w,)

        for (i, w2) in enumerate(sums):
                if w2 and not s[i + v]:
                        s[i + v] = w2 + (w,)

        sums = s
        if s[-neg]:
                for x in s[-neg]:

                        d = dict([(x, r[x])])
                        file('dict.txt','a'+'\n').write(repr(d))


                break

1 个答案:

答案 0 :(得分:0)

f = open('dict.txt','r')
filedata = f.read()
f.close()

newdata = filedata.replace("}{",", ")

f = open('lexiko.txt','w')
f.write(newdata)
f.close()

di = eval(open("lexiko.txt").read())

print di

这样做