Python“.write”

发布于 2024-10-06 03:48:41 字数 589 浏览 0 评论 0原文

我怎样才能“转换”这个:

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))

How can I "convert" this:

input=[(0.25  , 'x1'),(0.20 , 'x2'), ............................]

so that I can write in test.txt only this:
x1=0.25, x2=0.20, x3= ..................

f = open('test.txt', 'w')

f.write(input)

f.close()

I know for print, this work ok:

 print ' '.join("%s=%s" % (y, x) for x,y in input)

but I can't "import" into f.write(...)


Edit: Thanks to all, all worked, I didn't remember that I can use:

f.write(' '.join("%s=%s" % (y, x) for x,y in input))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

她如夕阳 2024-10-13 03:48:41

有什么问题吗

f.write(' '.join("%s=%s" % (y, x) for x,y in input))

或者

print >>f, ' '.join("%s=%s" % (y, x) for x,y in input)

有一个细微的区别,前者不会有尾随换行符。不过很容易修复。

Anything wrong with

f.write(' '.join("%s=%s" % (y, x) for x,y in input))

?

Or

print >>f, ' '.join("%s=%s" % (y, x) for x,y in input)

There's a minor difference in that the former won't have a trailing newline character. Easily fixed though.

有木有妳兜一样 2024-10-13 03:48:41

您有 ' '.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()

You have ' '.join("%s=%s" % (y, x) for x,y in input) and you are printing that; pass that to f.write, it's a perfectly valid expression:

f = open('text.txt', 'w')
f.write(' '.join("%s=%s" % (y, x) for x,y in input))
f.close()
剧终人散尽 2024-10-13 03:48:41

试试这个:

s = ', '.join("%s=%s" % (y, x) for x,y in input)
f.write(s)

Try this:

s = ', '.join("%s=%s" % (y, x) for x,y in input)
f.write(s)
微暖i 2024-10-13 03:48:41

如果您想使用 print,正确的方法取决于您使用的 Python 版本。对于 Python 2:

print >>f, ' '.join("%s=%s" % (y, x) for x,y in input)

对于 Python 3:

print(' '.join("%s=%s" % (y, x) for x,y in input), file=f)

If you would like to use print, the correct method depends on which version of Python you are using. For Python 2:

print >>f, ' '.join("%s=%s" % (y, x) for x,y in input)

For Python 3:

print(' '.join("%s=%s" % (y, x) for x,y in input), file=f)
芯好空 2024-10-13 03:48:41
  1. 不要命名任何 input,因为这是一个内置函数。
  2. 您只需将 print 语句替换为 f.write(' '.join("% s=%s" % (y, x) for x, y in myList) 它应该工作得很好
  1. Don't name anything input since that is a built-in function
  2. You can simply replace your print statement with f.write(' '.join("%s=%s" % (y, x) for x, y in myList) and it should work just fine
厌味 2024-10-13 03:48:41
f = open('test.txt', 'w')
f.write(' '.join("%s=%s" % (y, x) for x,y in input))
f.close()
f = open('test.txt', 'w')
f.write(' '.join("%s=%s" % (y, x) for x,y in input))
f.close()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文