gpt4 book ai didi

python - 写入命令不会在同一行上打印所有内容

转载 作者:太空宇宙 更新时间:2023-11-04 11:09:53 25 4
gpt4 key购买 nike

我在循环中使用以下命令打开 .txt 文件并在文件中写入一些结果。

with open ('results.txt', 'a') as file: 
file.write('%s %d %s %s \n' %(timestamp, v, str(dcur), str(gcur)))

但是,结果不会打印在 .txt 文件的同一行上,而 str(gcur) 会出现在下一行。

为什么会发生这种情况,如何解决?

最佳答案

很可能是因为当调用 strdcur 时,它以某种方式添加了一个 "\n"

你可以剥离它:

with open ('results.txt', 'a') as file: 
file.write('%s %d %s %s \n'.format(timestamp, v, str(dcur).rstript("\n"), str(gcur).rstript("\n")))

关于python - 写入命令不会在同一行上打印所有内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58538757/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com