Alan Isaac wrote:
Use index instead?
yup, that'll work. enclosed is another test file, with that and one
using string.split(comments) instead.
-Chris
--
Christopher Barker, Ph.D.
Oceanographer
Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception
[EMAIL PROTECTED]
#!/usr/bin/env python
"""
test of loadtext issue
"""
comments = "#"
SampleLines = [" 1 2 3 4 5\n",
" 1 2 3 4 5",
" 1 2 3 4 5#",
" # 1 2 3 4 5",
]
#SampleLines = ["a line with a comment # this is the comment"
# "# a comment-only line",
# " a line with no comment, and no newline",
# " a line with a trailing comment character, and no newline#",
# ]
print "\nold way -- this fails with no comment of newline"
for line in SampleLines:
print "input line: ", repr(line)
line = line[:line.find(comments)].strip()
print "output line:", repr(line)
print "\nwith regular expression:"
import re
pattern = re.compile(r"""
^\s* # leading white space
(.*) # Data
%s? # Zero or one comment character
(.*) # Comments
\s*$ # Trailing white space
"""%comments, re.VERBOSE)
match = pattern.search(line)
line, comment = match.groups()
for line in SampleLines:
print "input line: ", repr(line)
match = pattern.search(line)
line, comment = match.groups()
print "output line:", repr(line)
print "\nsimply pad the line with a space:"
for line in SampleLines:
print "input line: ", repr(line)
line += " "
line = line[:(line).find(comments)].strip()
print "output line:", repr(line)
print "\ntest for comment not found:"
for line in SampleLines:
print "input line: ", repr(line)
i = line.find(comments)
if i == -1:
line = line.strip()
else:
line = line[:i].strip()
print "output line:", repr(line)
print "\nuse string.split()"
for line in SampleLines:
print "input line: ", repr(line)
line = line.strip().split(comments)[0]
print "output line:", repr(line)
print "\nuse string.index"
print "\nold way -- this fails with no comment of newline"
for line in SampleLines:
print "input line: ", repr(line)
try:
line = line[:line.index(comments)].strip()
except ValueError:
line = line.strip()
print "output line:", repr(line)
_______________________________________________
Numpy-discussion mailing list
[email protected]
http://projects.scipy.org/mailman/listinfo/numpy-discussion