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

后端 未结 8 1164
被撕碎了的回忆
被撕碎了的回忆 2021-02-06 22:13

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.         


        
相关标签:
8条回答
  • 2021-02-06 22:54

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

    line.split(";")[0]
    
    0 讨论(0)
  • 2021-02-06 22:58

    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")
    
    0 讨论(0)
提交回复
热议问题