views:

585

answers:

9

Hi,

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'
+7  A: 

just do a split on the line by comment then get the first element eg

line.split(";")[0]
ghostdog74
+33  A: 

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.

Eli Courtwright
+1 You could use 1 for maxsplit param to be perfect
Jiri
+1 for answer and good addition by Jiri
bastianneu
To be honest, I think it's outright appalling that this answer was accepted.
SilentGhost
and upvoted so many times.
SilentGhost
@SilentGhost: Why? Is there some problem with the answer? Is there a better way?
Eli Courtwright
there's obviously a better way, which you can find on this page. your answer wasn't first and I don't really see how it deserves **29** upvotes + acceptance.
SilentGhost
*obviously*? Do tell. This is pretty simple, and I don't see any problems with it.
lfaraone
+3  A: 

For Python 2.5 or greater, I would use the partition method:

rtr = line.partition(';')[0].rstrip() + '\n'
Sinan Ünür
not available for version <2.5++
ghostdog74
@ghostdog74: stable versions of python are 2.6 and 3.1
SilentGhost
@SG, that's fine, but still, if one is still using <2.5++ in production, they will not have this luxury.
ghostdog74
This answer was valid when I posted it because the OP had not yet mentioned that he was using Python 2.4 (now about five years old). I am not going to delete this answer because I find the fact that `partition` returns a fixed length vector a useful feature, although it does not matter much here.
Sinan Ünür
A: 

Is this file always in this format? If so, use a regular expression to replace the strings.

although its one way, but its certainly not needed for this simple task
ghostdog74
+2  A: 
file = open(r'c:\temp\test.txt', 'r')
for line in file:   print
   line.split(";")[0].strip()
the empirical programmer
fix your syntax
John Machin
+1  A: 

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:\temp\test.txt', 'r'))
hughdbrown
A: 

I have not tested this with python but I use similar code else where.

import re
content = open(r'c:\temp\test.txt', 'r').read()
content = re.sub(";.+", "\n")
Matthew
your re.sub() is missing an argument and thus won't run -- very fortunate since **it would trash the first ';' in the file and everything after it**
John Machin
+2  A: 

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.

rtr = line.split(";", 1)[0].rstrip() + '\n'

Links to Documentation:

tgray
links you provide and methods you use are not the same
SilentGhost
A: 

Here is another way :

In [6]: line = "foo;bar"
In [7]: line[:line.find(";")] + "\n"
Out[7]: 'foo\n'
Chmouel Boudjnah
if line == "fubar", that produces "fuba\n" ... correcting the problem in a one-liner produces this: `line[:None if line.find(";") == -1 else line.find(";")]` (which I'm certainly not proposing as a good idea at all).
John Machin