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

8

47

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'
Swainson answered 24/7, 2009 at 15:15 Comment(3)
Yes, python2.4. Should have mentioned that in the ??Swainson
well python2.4 is two versions behind current stable version. what do you think?Twill
possible duplicate of How to remove all characters after a specific character in python?Horlacher
C
140

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.

Carn answered 24/7, 2009 at 15:18 Comment(1)
I agree with @Jiri in using the maxsplit arg, i.e line.split(";", 1)[0]. It's faster (especially if you are parsing lots of text) because you don't end up having to split the entire text just for getting the first element.Gaulin
K
20

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

line.split(";")[0]
Kloof answered 24/7, 2009 at 15:18 Comment(0)
H
4

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

rtr = line.partition(';')[0].rstrip() + '\n'
Houdon answered 24/7, 2009 at 15:19 Comment(5)
@ghostdog74: stable versions of python are 2.6 and 3.1Twill
@SG, that's fine, but still, if one is still using <2.5++ in production, they will not have this luxury.Kloof
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.Saransk
Why is partition better?Creodont
@MattFletcher Think about what happens when you parse a line with thousands of fields and you just want the first.Saransk
H
4

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:

Hullda answered 24/7, 2009 at 17:46 Comment(1)
links you provide and methods you use are not the sameTwill
I
3
file = open(r'c:\temp\test.txt', 'r')
for line in file:   print
   line.split(";")[0].strip()
Indecision answered 24/7, 2009 at 15:27 Comment(0)
A
1

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'))
Ainu answered 24/7, 2009 at 15:46 Comment(0)
O
1

Here is another way :

In [6]: line = "foo;bar"
In [7]: line[:line.find(";")] + "\n"
Out[7]: 'foo\n'
Oscillatory answered 25/7, 2009 at 23:34 Comment(1)
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).Bordure
F
-3

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")
Feune answered 24/7, 2009 at 16:6 Comment(1)
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 itBordure

© 2022 - 2024 — McMap. All rights reserved.