Let's say I'm parsing a file, which uses ;
as the comment character. I don't want to parse comments. So if I a line looks like this:
example.com. 600 IN MX 8 s1b9.example.net ; hello!
Is there an easier/more-elegant way to strip chars out other than this:
rtr = ''
for line in file:
trig = False
for char in line:
if not trig and char != ';':
rtr += char
else:
trig = True
if rtr[max(rtr)] != '\n':
rtr += '\n'
just do a split on the line by comment then get the first element eg
I'd recommend saying
which will give you a string of all characters up to but not including the first ";" character. If no ";" character is present, then it will give you the entire line.
For Python 2.5 or greater, I would use the
partition
method:Here is another way :
So you'll want to split the line on the first semicolon, take everything before it, strip off any lingering whitespace, and append a newline character.
Links to Documentation: