databasepath='/Users/software/database.txt'
sinfo=[]
soi = pnevli[1].header['OBJECT']
with open(databasepath) as fp:
for line in fp:
if soi in line:
line_array = line.split(sep=None, maxsplit=-1)
sinfo.append(line_array)
sinfo = np.array(sinfo)[0]
source = {
'Name': sinfo[1],
'PSR': sinfo[2],
'RA': sinfo[3],
'DEC': sinfo[5],
'RAJD': sinfo[11],
'DECJD': sinfo[13]
}
print(source)
savepath = obspath/obsid
file1 = open(str(savepath)+'/'+'PSR_'+soi+'.txt',"w")
file1.write(str(source))
file1.close()
#if not soi in line:
#print('The source is not listed')
I have written the above code in which it reads a header in a fits file and compares it to a database (database.txt) and if it finds a match, it copies the entire row of that database and writes it in a new text file. I have two problems here:
First problem: Lets say, the database has 1000 rows and it finds a match, if I use "if not soi in line:" or "else:", it will copy of the matched row to a text file and then prints 999 times The source is not listed
because it is in a for a loop. What I actually want is to write this statement (once) only if there is no match!
Second problem: When it writes the row in the text file, I would like it to write each string in a new line (for instance 'Name': sinfo[1] first line, 'PSR': sinfo[2] second line and so on, but I don't know how exactly to insert \n so it could jumpy to a new line!
Aucun commentaire:
Enregistrer un commentaire