[英]Write numerical parameters lines in text file in Ipython using writelines?
I am trying to write lines in software (spaceclaim) that has IPython embedded, perhaps with not with all libraries available.我正在尝试在嵌入了 IPython 的软件(spaceclaim)中编写行,可能没有所有可用的库。
I am trying to write two parameters in two rows under each other, but since they are numbers, I should use str(a)
for them, but I can not use \n
between the two parameters to change the line.我正在尝试将两个参数写在彼此下方的两行中,但是由于它们是数字,因此我应该为它们使用
str(a)
,但是我不能在两个参数之间使用\n
来更改行。 What happens is that I can write only one parameter to a text file but if I want more than one, then Python concatenates all in one line and ignores the comma.发生的情况是我只能将一个参数写入文本文件,但如果我想要多个参数,则 Python 将所有参数连接在一行中并忽略逗号。
import os
loc=os.getcwd()
filename = loc1+ '\\params.txt'
tw= [str(A), str(B)]
outfile = open(filename, "w")
outfile.writelines(tw)
outfile.close()
Why do you make a list?你为什么要列一个清单? Just use a string.
只需使用一个字符串。
tw = str(A) + '\n' + str(B)
with open(filename, 'w')as outfile:
outfile.writelines(tw)
outfile.close()
And I just want to say: Open a file in w
mode means that when you write something it overwrites everything.我只想说:以
w
模式打开文件意味着当您编写某些内容时,它会覆盖所有内容。 Maybe your looking for a
which means append.也许您正在寻找
a
意思是 append。
Second you have loc =os.getcwd()
and then you use filename = loc**1** + '\\params.txt'
其次你有
loc =os.getcwd()
然后你使用filename = loc**1** + '\\params.txt'
And more little thing.还有更多的小东西。 You can use
/
instead of \\
in a path.您可以在路径中使用
/
而不是\\
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.