Alex Rivera | Logout

In Python 2.4, how can I strip out characters after ';'?

Asked 2009-07-24T15:15:17.493
48

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'
Edit
Report

1 Answer

3
file = open(r'c:\temp\test.txt', 'r')
for line in file:   print
   line.split(";")[0].strip()
answered 2009-07-24T15:27:49.843

Your Answer