如何使用Astropy ascii表读取器跳过行尾注释?

时间:2015-03-24 15:39:09

标签: astropy

我想读一个包含Astropy表中行尾注释的ascii文件。像这样:

a | b
1 | 2
3 | 4 # this is a comment
# another comment
5 | 6

如图here所示,这并不是我想要的:

Table.read(filename, format='ascii.csv', delimiter='|', comment='\s*#')

我需要将哪些选项传递给Table.read才能使其正常工作?

1 个答案:

答案 0 :(得分:3)

没有内置方法可以做到这一点,但它只需要几行自定义代码。基本思想是创建CSV reader的子类,用你自己的Data类覆盖data_class = CsvData,它本身是CsvData的子类。在您的Data子类中,覆盖process_lines方法以执行自定义注释行处理。查看BaseData中的core.py课程,了解基线process_lines行为。

from astropy.io.ascii.basic import Csv, CsvData

class CsvCommentData(CsvData):
    comment = '#'  # regular expression not allowed for this reader

    def _strip_trailing_comments(self, lines):
        for line in lines:
            index = line.rfind(self.comment)
            if index >= 0:
                line = line[:index]
            yield line

    def process_lines(self, lines):
        return super(CsvCommentData, self).process_lines(self._strip_trailing_comments(lines))

class CsvComment(Csv):
    _format_name = 'csv_comment'
    _io_registry_can_write = True
    _description = 'Comma-separated-values with trailing comments allowed'

    data_class = CsvCommentData