Skip to content

Commit 1f7cb64

Browse files
taldcrofteteq
authored andcommitted
Merge pull request #5427 from taldcroft/ascii-aastex-ending
Fix problem reading AASTex table without trailing backslashes at end
1 parent ef7cd38 commit 1f7cb64

3 files changed

Lines changed: 53 additions & 3 deletions

File tree

CHANGES.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1777,6 +1777,9 @@ Bug Fixes
17771777

17781778
- Fix problem reading a zero-length ECSV table with a bool type column. [#5010]
17791779

1780+
- Fix problem reading an AASTex format table that does not have ``\\``
1781+
at the end of the last table row. [#5427]
1782+
17801783
- ``astropy.io.fits``
17811784

17821785
- Fix convenience functions (``getdata``, ``getheader``, ``append``,

astropy/io/ascii/latex.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
}
3333

3434

35+
RE_COMMENT = re.compile(r'(?<!\\)%') # % character but not \%
36+
3537
def add_dictval_to_list(adict, key, alist):
3638
'''
3739
Add a value from a dictionary to a list
@@ -80,13 +82,21 @@ class LatexSplitter(core.BaseSplitter):
8082
'''
8183
delimiter = '&'
8284

85+
def __call__(self, lines):
86+
last_line = RE_COMMENT.split(lines[-1])[0].strip()
87+
if not last_line.endswith(r'\\'):
88+
print(last_line)
89+
lines[-1] = last_line + r'\\'
90+
91+
return super(LatexSplitter, self).__call__(lines)
92+
8393
def process_line(self, line):
8494
"""Remove whitespace at the beginning or end of line. Also remove
8595
\\ at end of line"""
86-
line = line.split('%')[0]
96+
line = RE_COMMENT.split(line)[0]
8797
line = line.strip()
88-
if line[-2:] == r'\\':
89-
line = line.strip(r'\\')
98+
if line.endswith(r'\\'):
99+
line = line.rstrip(r'\\')
90100
else:
91101
raise core.InconsistentTableError(r'Lines in LaTeX table have to end with \\')
92102
return line
@@ -314,6 +324,9 @@ class AASTexHeaderSplitter(LatexSplitter):
314324
315325
\tablehead{\colhead{col1} & ... & \colhead{coln}}
316326
'''
327+
def __call__(self, lines):
328+
return super(LatexSplitter, self).__call__(lines)
329+
317330
def process_line(self, line):
318331
"""extract column names from tablehead
319332
"""

astropy/io/ascii/tests/test_read.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1025,3 +1025,37 @@ def test_table_with_no_newline():
10251025
t = ascii.read(table, **kwargs)
10261026
assert t.colnames == ['a', 'b']
10271027
assert len(t) == 0
1028+
1029+
def test_latex_no_trailing_backslash():
1030+
"""
1031+
Test that latex/aastex file with no trailing backslash can be read.
1032+
"""
1033+
lines = r"""
1034+
\begin{table}
1035+
\begin{tabular}{ccc}
1036+
a & b & c \\
1037+
1 & 1.0 & c \\ % comment
1038+
3\% & 3.0 & e % comment
1039+
\end{tabular}
1040+
\end{table}
1041+
"""
1042+
dat = ascii.read(lines, format='latex')
1043+
assert dat.colnames == ['a', 'b', 'c']
1044+
assert np.all(dat['a'] == ['1', r'3\%'])
1045+
assert np.all(dat['c'] == ['c', 'e'])
1046+
1047+
def text_aastex_no_trailing_backslash():
1048+
lines = r"""
1049+
\begin{deluxetable}{ccc}
1050+
\tablehead{\colhead{a} & \colhead{b} & \colhead{c}}
1051+
\startdata
1052+
1 & 1.0 & c \\
1053+
2 & 2.0 & d \\ % comment
1054+
3\% & 3.0 & e % comment
1055+
\enddata
1056+
\end{deluxetable}
1057+
"""
1058+
dat = ascii.read(lines, format='aastex')
1059+
assert dat.colnames == ['a', 'b', 'c']
1060+
assert np.all(dat['a'] == ['1', r'3\%'])
1061+
assert np.all(dat['c'] == ['c', 'e'])

0 commit comments

Comments
 (0)