[AstroPy] ascii read question

Derek Homeier derek at astro.physik.uni-goettingen.de
Tue Jan 31 12:22:12 EST 2017


> On 31 Jan 2017, at 5:51 PM, Derek Homeier <derek at astro.physik.uni-goettingen.de> wrote:
> 
>> I'm new to the AstroPy community, but I find the ascii read function very useful for ascii data import. However, I run into errors reading my table, probably due to the fact that I'm not quite familiar with all supported formats. 
>> 
>> I attach my table below, can somebody give me a hint how to read in all column names correctly? 
>> 
>> I have tried this when guessing failed: 
>> a = ascii.read("199808041.AHA", format = "basic", delimiter = " ", guess = False), but I don't know what to do with the "[ " in the header. 
>> 
> the separate ‘[ ‘ instances cause a mismatch between the header and data column numbers, so you’d need
> to get rid of either the blanks separating them from the actual names, or of the brackets altogether.
> The read() function does provide a ‘header_Splitter’ keyword for some custom pre- and postprocessing of
> the header lines - see http://docs.astropy.org/en/stable/api/astropy.io.ascii.BaseSplitter.html
> but it seems to use it you need to do a little Class exercise:
> 
> class mysplitter(astropy.io.ascii.BaseSplitter):
>     def __init__(self):
>         self.process_line = lambda x: x.translate({ord('['):'', ord(']'):“})
> 
Just a follow-up to self (no pun intended, but I am quite a dilettante when it comes to classes) -
This may indeed be a slightly cleaner option since it should not override the parent class’s initialisation
(and of course stripping the ‘]’ is entirely optional here and just for a cleaner look):

class mysplitter(astropy.io.ascii.BaseSplitter):
    def process_line(self, line):
        return line.translate({ord('['):'', ord(']'):''})

> Note that you have to remove the brackets from the header line before it is split by the whitespace. Then
> 
> a = ascii.read("199808041.AHA", format = "basic", delimiter = " ", guess = False, header_Splitter = mysplitter)
> 
> should work, but create column names entirely without the brackets.
> Don’t know if anyone has an idea how to do this in a more straightforward manner...

Cheers,
					Derek




More information about the AstroPy mailing list