Skip one line - python open with

3.3k views Asked by At

How I can skip first header line? I have headers duplicated later in the code, so I can eliminate them by if not l.startswith('MANDT') but first header I want to keep. How I need to modify the code?

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

Code I am using.

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

There are 6 answers

2
Shiva Gupta On

If your motto is to remove all lines starting with the keyword MANDT, except for the first line then this will work fine.

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)
0
Ajax1234 On

You can try this:

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

I hope I'm getting your question right. You could do something like this:

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')
1
Jonas Eriksson On

Just use slicing:

for l in lines[1:]:
  # do stuff
0
Chris On

There are lots of ways. I might start with having a simple variable that tracks whether the first header row has been seen or not.

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
0
Laurent LAPORTE On

You can strip the headers like this:

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)

You get:

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

Of course, you can simplify by using an index:

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

You get the same result.