44

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'
lfaraone
  • 49,562
  • 17
  • 52
  • 70
  • Yes, python2.4. Should have mentioned that in the ?? – lfaraone Jul 24 '09 at 19:42
  • 3
    well python2.4 is **two** versions behind current stable version. what do you think? – SilentGhost Jul 24 '09 at 19:47
  • 2
    possible duplicate of [How to remove all characters after a specific character in python?](http://stackoverflow.com/questions/904746/how-to-remove-all-characters-after-a-specific-character-in-python) – Mr. Bultitude Sep 15 '15 at 19:14

8 Answers8

137

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
  • 186,300
  • 67
  • 213
  • 256
  • 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. – Eddy Nov 05 '18 at 16:24
19

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

line.split(";")[0]
ghostdog74
  • 327,991
  • 56
  • 259
  • 343
4

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

rtr = line.partition(';')[0].rstrip() + '\n'
Sinan Ünür
  • 116,958
  • 15
  • 196
  • 339
  • 2
    @ghostdog74: stable versions of python are 2.6 and 3.1 – SilentGhost Jul 24 '09 at 15:29
  • 1
    @SG, that's fine, but still, if one is still using <2.5++ in production, they will not have this luxury. – ghostdog74 Jul 24 '09 at 16:03
  • 1
    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 Jul 25 '09 at 11:54
  • Why is `partition` better? – Matt Fletcher Dec 14 '17 at 10:33
  • @MattFletcher Think about what happens when you parse a line with thousands of fields and you just want the first. – Sinan Ünür Dec 21 '17 at 21:06
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:

Dean Coakley
  • 1,675
  • 2
  • 11
  • 25
tgray
  • 8,826
  • 5
  • 36
  • 41
3
file = open(r'c:\temp\test.txt', 'r')
for line in file:   print
   line.split(";")[0].strip()
Gern Blanston
  • 42,482
  • 19
  • 50
  • 64
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'))
hughdbrown
  • 47,733
  • 20
  • 85
  • 108
1

Here is another way :

In [6]: line = "foo;bar"
In [7]: line[:line.find(";")] + "\n"
Out[7]: 'foo\n'
Chmouel Boudjnah
  • 2,541
  • 3
  • 24
  • 28
  • 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 Jul 26 '09 at 00:06
-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")
Matthew FL
  • 1,612
  • 3
  • 18
  • 22
  • 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 it** – John Machin Jul 24 '09 at 16:40