Hi pythonians
I am trying to sort a Numpy array by field, but I am having trouble setting up the fields. I think I may have the dtype wrong. I keep getting a TypeError: "expected a readable buffer object".

Here is a sample of the infile:
1480 CL 9 2004 YEB4REDB 49 SY 1 S0111P 1
1142 CL 14 2005 ORA8PURE 10 6Y 0 S0212N 0
1350 CL 72 2006 A4LGLGWH 1 HY 0.1 S0212N 0
442 CL 7 1994 A8WHDBDG 1 HY 0.1 S0212N 0
1096 CL 13 1995 YEA4DGPU 1 HY 0.1 S0212N 0

problem code:

from numpy import *

infile = open("C:\Python25\Drew\CL_work\idage.txt", 'r')   
outfile= open("CLrcwarray.txt",'w')


rcw = infile.readlines()
bird_a = []
known = []
ct=0 #counts all birds in file
cta=0 #counts birds with ages between 0 and 2 "known ages"
ct1=0 #count for CLrcw creation
ct2=0 #count for CLrcw2 creation
CLrcw=zeros((4813,6),float) #array of all birds
CLrcw2=zeros((2715,6),float)#array of birds with known ages

for record in rcw:
    bird = record.strip().split()
    ct+=1
    age1 = float(bird[7])
    if age1 > 0:
        cta+=1
    infile.close()

infile = open("C:\Python25\Drew\CL_work\idage.txt", 'r')
for record in rcw:
    
    bird = record.strip().split()

    nID= float(bird[0])
    ter= float(bird[2])
    year= float(bird[3])
    stat= float(bird[5])
    fled= float(bird[9])
    age= float(bird[7])
  
    
    CLrcw[ct1,0]=nID    ## Values are assigned to CLrcw array
    CLrcw[ct1,1]=ter
    CLrcw[ct1,2]=year
    CLrcw[ct1,3]=stat
    CLrcw[ct1,4]=fled
    CLrcw[ct1,5]=age  
    ct1+=1

    if age > 0:
        CLrcw2[ct2,0]=nID    
        CLrcw2[ct2,1]=ter
        CLrcw2[ct2,2]=year
        CLrcw2[ct2,3]=stat
        CLrcw2[ct2,4]=fled
        CLrcw2[ct2,5]=age

        ct2+=1             
    
infile.close()

for record in CLrcw2:      
    birdid = record[0]
    c_yr = record[2]
    c_age = record[5]
    for each in CLrcw:      
        birdid2 = each[0]
        age2 = each[5]        
        if birdid == birdid2 and age2 == 0:  
            p_yr = each[2]
            
            if c_yr > p_yr:                 
                age3 = c_yr-p_yr+c_age
                each[5] = round_(age3,0)
            elif c_yr < p_yr:
                age3 = p_yr-c_yr+c_age
                each[5] = round_(age3,0)


y={'names' : ('nID','ter', 'year','stat','fled','age'),'formats': ('f4','f4','f4','f4','f4','f4')}
x=[('nID',float),('ter',float),('year',float),('stat',float),('fled',float),('age',float)]

CLrcw3 = asarray(CLrcw,dtype=x)

for each in sort(CLrcw3,order=['year']):
    set_printoptions(precision=1,suppress=True)
    a=str(each) [1:-1] 
    outfile.writelines("%s \n" %(a))
outfile.close()

y and x are the two data types I have tried.

Thanks in advance, this is a great learning website.

I figured it out on my own, but wanted to thank anyone who took a look at this problem.

I added this near the end of my code posted above, compare the two and you will figure it out.

x=[('nID',float),('ter',float),('year',float),('stat',float),('fled',float),('age',float)]


CLrcw3 = array([(a,b,c,d,e,f) for (a,b,c,d,e,f) in CLrcw], dtype=x)

for each in sort(CLrcw3,order=('nID','year')):
    set_printoptions(precision=1,suppress=True)
    a=str(each) [1:-1] 
    outfile.writelines("%s \n" %(a))
outfile.close()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.