我在这里搜索了许多关于删除字符串前两行的线程,但我似乎无法让它适用于我尝试过的每个解决方案。
这是我的字符串的样子:
version 1.00
6992
[-4.32063, -9.1198, -106.59][0.00064, 0.99993, -0.01210][etc...]
我想为我正在使用的脚本删除此 Roblox 网格文件的前两行。我该怎么做?
你的结尾角色是什么,但是像这样的东西呢
postString = inputString.split("n",2)[2]
结尾字符可能需要转义,但这就是我要开始的。
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:])
你可以简单地这样做。
''.join(x.splitlines(keepends=True)[2:])
splitlines
生成字符串列表。如果给出keepends=True
,则换行符将包含在生成的列表中l
并且''.join(l)
可用于重现原始字符串。
请注意,splitlines
适用于许多不同的线边界,例如u2028
>>> x = 'au2028bu2028cu2028'
>>> ''.join(x.splitlines(keepends=True)[2:])
'cu2028'
虽然split('n')
在这种情况下失败:
>>> x = 'au2028bu2028cu2028'
>>> x.split('n',2)[2]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list index out of range
<小时 />另请注意,如果在空字符串或以换行符结尾的字符串上调用 splitlines
和 split('n')
,则它们的行为会有所不同。比较以下示例(从splitlines
的文档复制):
>>> "".splitlines()
[]
>>> "One linen".splitlines()
['One line']
>>> ''.split('n')
['']
>>> 'Two linesn'.split('n')
['Two lines', '']
但是,如果给出keepends=True
,则会保留尾随换行符:
>>> "One linen".splitlines(keepends=True)
['One linen']
<小时 />更多示例和splitlines
视为线边界的列表可以在这里找到:https://docs.python.org/3/library/stdtypes.html?highlight=split#str.splitlines
删除带有 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]
我宁愿不拆分字符串以防字符串很大,并在之后保持换行符类型。
删除前 n 行:
def find_nth(haystack, needle, n):
start = haystack.find(needle)
while start >= 0 and n > 1:
start = haystack.find(needle, start+len(needle))
n -= 1
return start
assert s[find_nth(s, 'n', 2) + 1:] == 'cndn'
另请参阅:查找字符串中子字符串的第 n 个匹配项
或者只删除一个:
s = 'anbncndn'
assert s[s.find('n') + 1:] == 'bncndn'
在Python 3.6.6上测试。
我有用:
first_line = text.find('n') + 1
second_line = text.find('n', first_line) + 1
text = text[second_line:]
一些规则,例如仅当这些行以字符开头时才考虑这些行'['
lines = [line for line in lines if line.startswith('[')]
你可以找到 '' 的索引并首先忽略;然后,从主字符串中第二个""子字符串的末尾开始新字符串。
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:])
结果是:
$ 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.
$