从python中提取的txt文件提取的附加值时如何添加空格
在文本文件中,我有两个值(x和y)的列 该文件已读取,并使用空间进行了拆分,并将其附加在X和Y中。 但是,在附加x值时,它是连续的,没有任何空间。 如何在每个附加值中添加空间或使其出现在新行中。
import numpy as np
x = []
y = []
for line in open('IV.txt', 'r'):
lines = [i for i in line.split(" ")]
x.append(lines[0])
y.append(lines[1])
f1 = open('xdata.txt','a')
f1.writelines(x)
f1.close()
f2 = open('ydata.txt','a')
f2.writelines(y)
f2.close()
输入数据是
0 3
0.2 3.4
0.4 3.6
0.6 3.8
0.8 4.2
1 5.3
1.2 5.5
1.4 5.8
1.6 6.0
1.8 6.4
2 6.8
2.2 7.1
2.4 7.8
2.6 8.2
2.8 8.4
3 8.8
X的输出显示为 00.20.40.60.811.21.41.61.822.22.42.62.83
In a text file I have two columns of value (x and y)
The file was read and it was split using space and appended in x and y.
but while appending x values, it is continuous without any space.
How to add space or to make it appear in new line for every appended value.
import numpy as np
x = []
y = []
for line in open('IV.txt', 'r'):
lines = [i for i in line.split(" ")]
x.append(lines[0])
y.append(lines[1])
f1 = open('xdata.txt','a')
f1.writelines(x)
f1.close()
f2 = open('ydata.txt','a')
f2.writelines(y)
f2.close()
the input data is
0 3
0.2 3.4
0.4 3.6
0.6 3.8
0.8 4.2
1 5.3
1.2 5.5
1.4 5.8
1.6 6.0
1.8 6.4
2 6.8
2.2 7.1
2.4 7.8
2.6 8.2
2.8 8.4
3 8.8
So the output of x appears as
00.20.40.60.811.21.41.61.822.22.42.62.83
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以将空间或新线放在这样。
添加
您可以从这样的输入数据中获取X,Y数组。
You can put space or newline like this.
ADD
you can get x, y array from input data like this.
您需要清单吗?如果没有,我建议在不附加到列表的情况下直接写入文件。另外,我将使用带有语句而不是明确的打开和关闭文件。自Python 3.10以来,我们有括号内上下文管理者您像您一样简化了代码尝试使用以下语法写入。
Do you need the lists? If not, I suggest to write directly to the files without appending to the lists. Also, I would use the with statement instead of explicitly opening and closing the files. Since Python 3.10 we have parenthesized context managers that simplify code like the one you are trying to write with the following syntax.