在Python 2.4中,如何在';'之后去除字符? [英] In Python 2.4, how can I strip out characters after ';'?
本文介绍了在Python 2.4中,如何在';'之后去除字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我正在解析一个文件,该文件使用;
作为注释字符.我不想解析评论.因此,如果我一行看起来像这样:
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'
推荐答案
我建议您说
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.
这篇关于在Python 2.4中,如何在';'之后去除字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文