gpt4 book ai didi

python - 在 Python 2.4 中,如何去除 ';' 之后的字符?

转载 作者:IT老高 更新时间:2023-10-28 21:52:29 25 4
gpt4 key购买 nike

假设我正在解析一个文件,它使用 ; 作为注释字符。我不想解析评论。所以如果我的一行看起来像这样:

example.com.              600     IN      MX      8 s1b9.example.net ; hello!

除此之外还有更简单/更优雅的去除字符的方法:

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]

这将为您提供一个包含所有字符的字符串,但不包括第一个“;”特点。如果不 ”;”字符存在,那么它会给你整行。

关于python - 在 Python 2.4 中,如何去除 ';' 之后的字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1178335/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com