【发布时间】:2020-02-09 09:10:45
【问题描述】:
我在使用 Python 将长字符串打印到文件时遇到问题。具体来说,我使用下面的代码输出coords,这是一个numpy数组(10乘2)。
with open('MD_traj.yml', 'a+', newline='') as outfile:
outfile.write('# Output data of MD simulation\n')
outfile.write('x-coordinates: ' + str(coords[:, 0]) + '\n')
outfile.write('y-coordinates: ' + str(coords[:, 1]) + '\n')
我想要的输出文件是:
x-coordinates: [ 1.31142392 -1.10193486 -0.66411767 -0.98806056 -0.38443227 -0.99041216 0.99185667 -0.20955044 -0.17442841 1.43698767]
y-coordinates: [-1.2635609 0.50664106 1.0458195 -1.16822174 0.46595609 1.1952824 -0.87070535 0.4427565 -0.79005599 0.74077841]
但是,在我的输出文件中,这些行被分成两部分,这使得解析文件变得更加困难。如下图。
x-coordinates: [ 1.31142392 -1.10193486 -0.66411767 -0.98806056 -0.38443227 -0.99041216
0.99185667 -0.20955044 -0.17442841 1.43698767]
y-coordinates: [-1.2635609 0.50664106 1.0458195 -1.16822174 0.46595609 1.19528241
-0.87070535 0.4427565 -0.79005599 0.74077841]
有人可以就此提供建议吗?我花了很多时间寻找解决方案,但没有运气。提前非常感谢您!
【问题讨论】:
标签: python string line-breaks