In Python 2.4, how can I strip out characters after ';'?
Question:
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'
Answers:
just do a split on the line by comment then get the first element
eg
line.split(";")[0]
I’d recommend saying
line.split(";")[0]
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:
rtr = line.partition(';')[0].rstrip() + 'n'
file = open(r'c:temptest.txt', 'r')
for line in file: print
line.split(";")[0].strip()
Reading, splitting, stripping, and joining lines with newline all in one line of python:
rtr = 'n'.join(line.split(';')[0].strip() for line in open(r'c:temptest.txt', 'r'))
I have not tested this with python but I use similar code else where.
import re
content = open(r'c:temptest.txt', 'r').read()
content = re.sub(";.+", "n")
Here is another way :
In [6]: line = "foo;bar"
In [7]: line[:line.find(";")] + "n"
Out[7]: 'foon'
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
line.split(";")[0]
I’d recommend saying
line.split(";")[0]
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:
rtr = line.partition(';')[0].rstrip() + 'n'
file = open(r'c:temptest.txt', 'r')
for line in file: print
line.split(";")[0].strip()
Reading, splitting, stripping, and joining lines with newline all in one line of python:
rtr = 'n'.join(line.split(';')[0].strip() for line in open(r'c:temptest.txt', 'r'))
I have not tested this with python but I use similar code else where.
import re
content = open(r'c:temptest.txt', 'r').read()
content = re.sub(";.+", "n")
Here is another way :
In [6]: line = "foo;bar" In [7]: line[:line.find(";")] + "n" Out[7]: 'foon'