在python中向文件写入长字符串而不换行

在python中向文件写入长字符串而不换行,python,string,line-breaks,Python,String,Line Breaks,我在使用Python将长字符串打印到文件时遇到问题。具体来说,我使用以下代码输出coords,这是一个numpy数组(10 x 2) 在输出文件中我需要的是: 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.506

我在使用Python将长字符串打印到文件时遇到问题。具体来说,我使用以下代码输出
coords
,这是一个numpy数组(10 x 2)

在输出文件中我需要的是:

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]

有人能就此提出建议吗?我花了很多时间寻找解决方案,但没有运气。提前非常感谢

也许这一切都是为了实现NumPy的
\uuuu str\uuuu

这是我的密码。它解决了你的问题。只需使用NumPy对象中的方法
tolist()

将numpy导入为np
从随机导入随机
x=[random()表示范围(10)内的uu]
y=[random()表示范围(10)]
coords=np.vstack([x,y])
以open('data.yml','a+',换行='')作为输出文件:
coords_list=coords.tolist()
outfile.write(“#MD模拟的输出数据\n”)
write('x坐标:'+str(坐标列表[0])+'\n')
outfile.write('y坐标:'+str(坐标列表[1])+'\n')
结果

# Output data of MD simulation
x-coordinates: [0.8686902412164521, 0.478781961466336, 0.6641005825531633, 0.39111314403044306, 0.9438645478501313, 0.8371483392442387, 0.675984748690976, 0.7254844588305968, 0.7879460984009438, 0.7033985196947845]
y-coordinates: [0.8587330241195635, 0.4748353213357631, 0.20692421648029558, 0.8039948888725431, 0.9731648049162153, 0.7237063173464939, 0.8089361624221216, 0.16435387677097268, 0.944345230621302, 0.2901067965594649]
# Output data of MD simulation
x-coordinates: [0.8686902412164521, 0.478781961466336, 0.6641005825531633, 0.39111314403044306, 0.9438645478501313, 0.8371483392442387, 0.675984748690976, 0.7254844588305968, 0.7879460984009438, 0.7033985196947845]
y-coordinates: [0.8587330241195635, 0.4748353213357631, 0.20692421648029558, 0.8039948888725431, 0.9731648049162153, 0.7237063173464939, 0.8089361624221216, 0.16435387677097268, 0.944345230621302, 0.2901067965594649]