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'
8 个解决方案
#1
88
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.
它将为您提供一系列所有字符,但不包括第一个“;”字符。如果不 ”;”字符存在,然后它会给你整行。
#2
13
just do a split on the line by comment then get the first element eg
只需通过注释进行分割,然后获取第一个元素,例如
line.split(";")[0]
#3
4
For Python 2.5 or greater, I would use the partition
method:
对于Python 2.5或更高版本,我将使用分区方法:
rtr = line.partition(';')[0].rstrip() + '\n'
#4
3
file = open(r'c:\temp\test.txt', 'r')
for line in file: print
line.split(";")[0].strip()
#5
#6
1
Reading, splitting, stripping, and joining lines with newline all in one line of python:
在一行python中读取,拆分,剥离和连接换行符:
rtr = '\n'.join(line.split(';')[0].strip() for line in open(r'c:\temp\test.txt', 'r'))
#7
0
Here is another way :
这是另一种方式:
In [6]: line = "foo;bar" In [7]: line[:line.find(";")] + "\n" Out[7]: 'foo\n'
#8
-2
I have not tested this with python but I use similar code else where.
我没有用python测试过这个,但我在其他地方使用了类似的代码。
import re
content = open(r'c:\temp\test.txt', 'r').read()
content = re.sub(";.+", "\n")
#1
88
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.
它将为您提供一系列所有字符,但不包括第一个“;”字符。如果不 ”;”字符存在,然后它会给你整行。
#2
13
just do a split on the line by comment then get the first element eg
只需通过注释进行分割,然后获取第一个元素,例如
line.split(";")[0]
#3
4
For Python 2.5 or greater, I would use the partition
method:
对于Python 2.5或更高版本,我将使用分区方法:
rtr = line.partition(';')[0].rstrip() + '\n'
#4
3
file = open(r'c:\temp\test.txt', 'r')
for line in file: print
line.split(";")[0].strip()
#5
2
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:
文档链接:
#6
1
Reading, splitting, stripping, and joining lines with newline all in one line of python:
在一行python中读取,拆分,剥离和连接换行符:
rtr = '\n'.join(line.split(';')[0].strip() for line in open(r'c:\temp\test.txt', 'r'))
#7
0
Here is another way :
这是另一种方式:
In [6]: line = "foo;bar" In [7]: line[:line.find(";")] + "\n" Out[7]: 'foo\n'
#8
-2
I have not tested this with python but I use similar code else where.
我没有用python测试过这个,但我在其他地方使用了类似的代码。
import re
content = open(r'c:\temp\test.txt', 'r').read()
content = re.sub(";.+", "\n")