Python - 删除字符串的前两行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30833409/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python - Deleting the first 2 lines of a string
提问by GShocked
I've searched many threads here on removing the first two lines of a string but I can't seem to get it to work with every solution I've tried.
我在这里搜索了许多关于删除字符串前两行的线程,但我似乎无法让它与我尝试过的每个解决方案一起使用。
Here is what my string looks like:
这是我的字符串的样子:
version 1.00
6992
[-4.32063, -9.1198, -106.59][0.00064, 0.99993, -0.01210][etc...]
I want to remove the first two lines of this Roblox mesh file for a script I am using. How can I do that?
我想为我正在使用的脚本删除这个 Roblox 网格文件的前两行。我怎样才能做到这一点?
采纳答案by dstudeba
I don't know what your end character is, but what about something like
我不知道你的最终角色是什么,但是像
postString = inputString.split("\n",2)[2];
The end character might need to be escaped, but that is what I would start with.
结束字符可能需要转义,但这就是我要开始的。
回答by Daniel
Remove the lines with split
:
删除行split
:
lines = """version 1.00
6992
[-4.32063, -9.1198, -106.59][0.00064, 0.99993, -0.01210][etc...]"""
lines = lines.split('\n',2)[-1]
回答by vks
x="""version 1.00
6992
[-4.32063, -9.1198, -106.59][0.00064, 0.99993, -0.01210][etc...]
abc
asdda"""
print "\n".join(x.split("\n")[2:])
You can simply do this.
你可以简单地做到这一点。
回答by Bharadwaj
You could use some rules, like consider those lines only if they start with '['
character lines = [line for line in lines if line.startswith('[')]
您可以使用一些规则,例如仅在以'['
字符开头时才考虑这些行lines = [line for line in lines if line.startswith('[')]
回答by Saeed Zahedian Abroodi
You can find index of '\n'and ignore first; Then, start new string from end of second '\n'sub-string in main string.
您可以找到'\n' 的索引并先忽略;然后,从主字符串中第二个'\n'子字符串的末尾开始新字符串。
import re
def find_sub_string_index(string, sub_string, offset=0, ignore=0):
start = 0
swap = len(sub_string)
ignore += 1 # find first at least
try:
if start < 0:
return -1 # Not Found
if offset > 0:
# Start main string from offset (offset is begining of check)
string = string[offset:]
for i in range(ignore):
# swap: end of substring index
# start is end of sub-string index in main string
start += re.search(sub_string, string).start() + swap
string = string[start:]
return start
except:
return -1 # Got Error
string = """The first line.
The second line.
The third line.
The forth line.
The fifth line."""
sub_string = "\n"
ignore = 1 # Ignore times
start = find_sub_string_index(string, sub_string, ignore=1)
print("Finding sub-string '{0}' from main text.".format(sub_string))
print("Ignore {0} times.".format(ignore))
print("Start index:", start)
print("Result:")
print(string[start:])
The result is:
结果是:
$ python3 test.py
Finding sub-string '
' from main text.
Ignore 1 times.
Start index: 33
Result:
The third line.
The forth line.
The fifth line.
$
$
$
$ python3 test.py
Finding sub-string 'The' from main text.
Ignore 2 times.
Start index: 19
Result:
second line.
The third line.
The forth line.
The fifth line.
$