跳过一行 - 用python打开

时间:2017-09-13 14:27:00

标签: python readlines

如何跳过第一个标题行?我之后在代码中重复了标题,所以如果不是l.startswith(' MANDT')我可以消除它们,但我想保留第一个标题。我需要如何修改代码?

keep -> MANDT|BUKRS|NETWR|UMSKS|UMSKZ|AUGDT|AUGBL|ZUONR
100|1000|23.321-|||||TEXT
100|1000|0.12|||||TEXT
100|1500|90|||||TEXT
remove -> MANDT|BUKRS|NETWR|UMSKS|UMSKZ|AUGDT|AUGBL|ZUONR
100|1000|23.321-|||||TEXT
100|1000|0.12|||||TEXT
100|1500|90|||||TEXT
remove -> MANDT|BUKRS|NETWR|UMSKS|UMSKZ|AUGDT|AUGBL|ZUONR

我正在使用的代码。

with open('yourfile.txt', 'r+') as f:  # 'r+' - read/write mode
    lines = f.read().splitlines()
    f.seek(0)  # reset file pointer
    f.truncate()  # truncating file contents
    for l in lines:
        if not l.startswith('---'):
            # or f.write('|'.join(map(str.strip, l.strip('|').split('|'))) + '\n')
            f.write(re.sub(r'\|\s*|\s*\|', '|', l).strip('|') + '\n')

6 个答案:

答案 0 :(得分:2)

只需使用切片:

for l in lines[1:]:
  # do stuff

答案 1 :(得分:1)

有很多方法。我可以从一个简单的变量开始,该变量跟踪是否已经看到第一个标题行。

expected_header = 'MANDT|BUKRS...'

with open('yourfile.txt', 'r+') as f:   # 'r+' - read/write mode
    # ... get lines ...

header_seen = False
for l in lines:
    if l == expected_header:
        if header_seen:
            # do nothing, just skip to the next line in the file
            continue
        else:
             # act on this line, but remember not to parse further headers
            header_seen = True 
    # do something with the line here

答案 2 :(得分:0)

你可以试试这个:

f = [i.strip("\n") for i in open('filename.txt')]
new_file = [f[0]]+[i for i in f[1:] if i != f[0]]

答案 3 :(得分:0)

我希望我的问题是正确的。你可以这样做:

with open('yourfile.txt', 'r+') as f:   # 'r+' - read/write mode
lines = f.read().splitlines()
f.seek(0)      # reset file pointer
f.truncate()   # truncating file contents
isFirstLine = True
for l in lines:
    if isFirstLine:
        isFirstLine = False
        continue
    if not l.startswith('---') and :
        # or f.write('|'.join(map(str.strip, l.strip('|').split('|'))) + '\n')
        f.write(re.sub(r'\|\s*|\s*\|', '|', l).strip('|') + '\n')

答案 4 :(得分:0)

您可以像这样删除标题:

from __future__ import print_function

import io

lines = f.read().splitlines()
f.seek(0)
f.truncate()

header = None
for line in lines:
    if line.startswith(u"MANDT"):
        if header:
            continue
        else:
            header = line
            print(line, file=f)
    else:
        print(line, file=f)

你得到:

MANDT|BUKRS|NETWR|UMSKS|UMSKZ|AUGDT|AUGBL|ZUONR
100|1000|23.321-|||||TEXT
100|1000|0.12|||||TEXT
100|1500|90|||||TEXT
100|1000|23.321-|||||TEXT
100|1000|0.12|||||TEXT
100|1500|90|||||TEXT

当然,您可以使用索引进行简化:

for index, line in enumerate(lines):
    if not index or not line.startswith(u"MANDT"):
        print(line, file=f)

你得到的结果相同。

答案 5 :(得分:0)

如果你的座右铭是删除以关键字MANDT开头的所有行,除了第一行,那么这将正常工作。

with open('yourfile.txt') as f:
    data = f.readlines()

k = data[0]
for line in data:
    if line.startswith('MANDT'):
        data.remove(line)

with open('yourfile2.txt','w') as f:
    f.write(k + '/n')
    for line in data:
        f.write(line)