Python - 删除字符串的前2行

时间:2015-06-14 19:09:32

标签: python string file parsing lines

我在这里搜索了许多线程,删除了字符串的前两行,但我似乎无法使用我尝试的每个解决方案。

这是我的字符串的样子:

version 1.00
6992
[-4.32063, -9.1198, -106.59][0.00064, 0.99993, -0.01210][etc...]

我想为我正在使用的脚本删除此Roblox网格文件的前两行。我怎么能这样做?

8 个答案:

答案 0 :(得分:20)

我不知道你的最终角色是什么,但是

之类的东西呢
postString = inputString.split("\n",2)[2];

结束角色可能需要转义,但这就是我要开始的。

答案 1 :(得分:6)

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:])

你可以这样做。

答案 2 :(得分:2)

删除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]

答案 3 :(得分:1)

您可以使用一些规则,例如只有在'['字符lines = [line for line in lines if line.startswith('[')]开头时才考虑这些行

答案 4 :(得分:1)

如果字符串很大,我宁愿不分割字符串,而是以后再换行。

删除前n行:

var output = "";
var fs = require('fs');
var lineReader = require('readline').createInterface({
  input: require('fs').createReadStream('file.txt')
});

lineReader.on('line', function (line) {
  if(CONDITON_TO_CHECK_FOR_UPDATE){

  }
  output += line + "\n";
});

lineReader.on('close', function (line) {
  fs.writeFile('file.txt', output, function(err, data){
    if (err) console.log(err);
    console.log("Successfully Written to File.");
});
});

另请参阅:Find the nth occurrence of substring in a string

或仅删除一个:

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:] == 'c\nd\n'

在Python 3.6.6上测试。

答案 5 :(得分:0)

您可以找到'\ 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:])
  

结果是:

$ 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.
$

答案 6 :(得分:0)

''.join(x.splitlines(keepends=True)[2:])

splitlines 生成一个字符串列表。如果给出 keepends=True,则结果列表中包含换行符 l''.join(l) 可用于重现原始字符串。


请注意,splitlines 适用于许多不同的线边界,例如 \u2028

>>> x = 'a\u2028b\u2028c\u2028'
>>> ''.join(x.splitlines(keepends=True)[2:])
'c\u2028'

while split('\n') 在这种情况下失败:

>>> x = 'a\u2028b\u2028c\u2028'
>>> x.split('\n',2)[2]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list index out of range

另请注意,如果在空字符串或以换行符结尾的字符串上调用 splitlinessplit('\n'),它们的行为会有所不同。比较以下示例(复制自 splitlines 的文档):

>>> "".splitlines()
[]
>>> "One line\n".splitlines()
['One line']

>>> ''.split('\n')
['']
>>> 'Two lines\n'.split('\n')
['Two lines', '']

但是,如果给出 keepends=True,则保留尾随换行符:

>>> "One line\n".splitlines(keepends=True)
['One line\n']

可以在此处找到更多示例和 splitlines 视为线边界的列表: https://docs.python.org/3/library/stdtypes.html?highlight=split#str.splitlines

答案 7 :(得分:0)

这对我有用:

model