0

I'm very new to python, but have been using it to calculate and filter through data. I'm trying to output my array so I can pass it to other programs, but the output is one solid piece of text, with brackets and commas separating it.

I understand there are ways of manipulating this, but I want to understand why my code has output it in this format, and how to make it output it in nice columns instead.

The array was generated with:

! /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

example output:

[(-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),

desired output:

[-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...

Example inputs:

example 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

Example 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 Answers 2

1

The problem is the multiple parenthesis when you append. You are appending tuples.

what you want is to be adding lists - i.e. the ones with square brackets.

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' is the data you provided made into a csv

Sign up to request clarification or add additional context in comments.

6 Comments

edited the question to include an example input (which is a truncated version of the full set).
edited again, so you should have all the information to run it
can you tell me where my answer isn't answering your question?
Sorry, I didn't see that. I was testing a few things while inputing the code and I guess the page didn't show me your edit... Trying it now in the exact code I've supplied above, and for the end argument I still get: [[-1, [1.22333], 0.0], [-1, [1.24223], 153.4651968428021], [-1, [1.48462], 148.59335545709976], [-1, [1.39778], 86.143305392816202]]
OK, I'll take another tack. I fail to see how using the print statement can change anything about the operation of your code. You aren't printing to a file. If you put parenthesis around things, you are creating a tuple, an immutable data type. Are you intentionally doing that?
|
0

I didn't really get an answer that enabled me to understand the problem, the one suggested above just prevented to whole code working properly. I did find a work around by including the print command in the loop defining my final output.

 for x in xrange(a-1):

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.