Python 无法以正确格式输出txt
这是我的密码。问题是输出看起来像这样Python 无法以正确格式输出txt,python,dictionary,subset,subset-sum,Python,Dictionary,Subset,Subset Sum,这是我的密码。问题是输出看起来像这样 2015-06-0319:32:11.225085 {'2015-01-21-20:56:45.mp3':1}{'negative':-2}{'2015-01-15-21:28:23.mp3':1} 我想成为一本字典。。。。就像下面这个,我可以把它作为字典读回,从第一个子集和中删除键,然后继续输出第二个子集和,依此类推,直到没有其他子集和存在 2015-06-0319:32:11.225085 {'2015-01-21-20:56:45.mp3':1,'ne
2015-06-0319:32:11.225085
{'2015-01-21-20:56:45.mp3':1}{'negative':-2}{'2015-01-15-21:28:23.mp3':1}
我想成为一本字典。。。。就像下面这个,我可以把它作为字典读回,从第一个子集和中删除键,然后继续输出第二个子集和,依此类推,直到没有其他子集和存在
2015-06-0319:32:11.225085
{'2015-01-21-20:56:45.mp3':1,'negative':-2,'2015-01-15-21:28:23.mp3':1}
有什么想法吗
先进的thanx
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
这就行了请不要投反对票,也不要说为什么!那太粗鲁了
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