【问题标题】:Cant make a txt output at proper format无法以正确的格式输出 txt
【发布时间】:2015-06-03 16:36:28
【问题描述】:

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

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

【问题讨论】:

  • 请不要投反对票,也不要说出原因!这是粗鲁的

标签: python dictionary subset subset-sum


【解决方案1】:
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

这样就可以了

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-10-05
    • 1970-01-01
    • 2014-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-21
    相关资源
    最近更新 更多