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

后端 未结 8 1306
被撕碎了的回忆
被撕碎了的回忆 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:45

    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:

    • split
    • rstrip

提交回复
热议问题