我如何“转换”这个:
input=[(0.25 , 'x1'),(0.20 , 'x2'), ............................]所以我只能用test.txt写这个: x1=0.25,x2=0.20,x3= .
f= open('test.txt','w')
F.write(输入)
f.close()
我知道印刷品,这件作品还好:
print ' '.join("%s=%s" % (y, x) for x,y in input)但我不能“导入”到f.write(.)
编辑:感谢所有的工作,我不记得我可以使用:
f.write(' '.join("%s=%s" % (y, x) for x,y in input))发布于 2010-12-05 20:41:37
您有' '.join("%s=%s" % (y, x) for x,y in input),您正在打印它;将它传递给f.write,它是一个非常有效的表达式:
f = open('text.txt', 'w')
f.write(' '.join("%s=%s" % (y, x) for x,y in input))
f.close()发布于 2010-12-05 20:40:54
任何有问题的事
f.write(' '.join("%s=%s" % (y, x) for x,y in input))或
print >>f, ' '.join("%s=%s" % (y, x) for x,y in input)有一个小的区别,前者不会有尾随换行符。不过很容易修好。
发布于 2010-12-05 20:41:04
试试这个:
s = ', '.join("%s=%s" % (y, x) for x,y in input)
f.write(s)https://stackoverflow.com/questions/4361021
复制相似问题