输出python / numpy数组作为列

时间:2016-01-17 11:10:25

标签: python arrays numpy

我对python很新,但一直用它来计算和过滤数据。我试图输出我的数组,所以我可以将它传递给其他程序,但输出是一段实心文本,用括号和逗号分隔它。

我知道有一些方法可以操作它,但我想了解为什么我的代码以这种格式输出它,以及如何让它以漂亮的列输出它。

使用以下代码生成数组:

! /usr/bin/env python

import numpy as np
import networkx
import gridData
from scipy.spatial.distance import euclidean

INPUT1=open("test_area.xvg",'r')
INPUT2=open("test_atom.xvg",'r')
OUTPUT1= open("negdist.txt",'w')

area = []
pointneg = []
posneg = []
negdistance =[ ]
negresarea = []

 while True:
      line = INPUT1.readline()
      if not line:
           break
      col = line.split()
      if col:
            area.append(((col[0]),float(col[1])))


 pointneg.append((-65.097000,5.079000,-9.843000))



while True:
    line = INPUT2.readline()
    if not line:
         break
    col = line.split()
    if col:
         pointneg.append((float(col[5]),float(col[6]),float(col[7])))
         posneg.append((col[4]))


 for col in posneg:
      negresarea.append(area[int(col)-1][1])


 a=len(pointneg)

 for x in xrange(a-1):

        negdistance.append((-1,(negresarea[x]),euclidean((pointneg[0]),(pointneg[x]))))

  print >> OUTPUT1, negdistance

示例输出:

[(-1, 1.22333, 0.0), (-1, 1.24223, 153.4651968428021), (-1, 1.48462, 148.59335545709976), (-1, 1.39778, 86.143305392816202), (-1, 0.932278, 47.914688322058403), (-1, 1.04997, 28.622555546282022),

期望的输出:

[-1, 1.22333, 0.0

-1, 1.24223, 153.4651968428021

-1, 1.48462, 148.59335545709976

-1, 1.39778, 86.143305392816202

-1, 0.932278, 47.914688322058403 

-1, 1.04997, 28.622555546282022...

示例输入:

示例input1

     1     2.12371          0
     2     1.05275          0
     3    0.865794          0
     4    0.933986          0
     5     1.09092          0
     6     1.22333          0
     7     1.54639          0
     8     1.24223          0
     9     1.10928          0
    10     1.16232          0
    11     0.60942          0
    12     1.40117          0
    13     1.58521          0
    14     1.00011          0
    15     1.18881          0
    16     1.68442          0
    17    0.866275          0
    18     1.79196          0
    19      1.4375          0
    20       1.198          0
    21     1.01645          0
    22     1.82221          0
    23     1.99409          0
    24      1.0728          0
    25    0.679654          0
    26     1.15578          0
    27     1.28326          0
    28     1.00451          0
    29     1.48462          0
    30     1.33399          0
    31     1.13697          0
    32     1.27483          0
    33     1.18738          0
    34     1.08141          0
    35     1.15163          0
    36     0.93699          0
    37    0.940171          0
    38     1.92887          0
    39     1.35721          0
    40     1.85447          0
    41     1.39778          0
    42     1.97309          0

示例Input2

ATOM     35  CA  GLU     6      56.838   -5.202 -102.459  1.00273.53           C
ATOM     55  CA  GLU     8      54.729   -6.650  -96.930  1.00262.73           C
ATOM    225  CA  GLU    29       5.407   -2.199  -58.801  1.00238.62           C
ATOM    321  CA  GLU    41      -24.633   -0.327  -34.928  1.00321.69           C

2 个答案:

答案 0 :(得分:1)

问题是附加时的多个括号。你正在附加元组。

你想要的是添加列表 - 即带方括号的列表。

import numpy as np
area = []
with open('example2.txt') as filehandle:
    for line in filehandle:
        if line.strip() == '':continue
        line = line.strip().split(',')
        area.append([int(line[0]),float(line[1]),float(line[2])])
    area = np.array(area)
print(area)

'example2.txt'是您提供给csv的数据

答案 1 :(得分:0)

我没有真正得到一个让我理解这个问题的答案,上面建议的那个只是阻止整个代码正常工作。我确实通过在定义我的最终输出的循环中包含print命令找到了解决方法。

Validator v = Validator.forLanguage(Languages.W3C_XML_SCHEMA_NS_URI);
v.setSchemaSources(Input.fromFile("local.xsd").build());
ValidationResult result = v.validateInstance(new StreamSource(new File("local.xml")));
return result.isValid();
相关问题