-3

I am trying to skip the first 23 lines of a txt file and read the data from line 24 onwards. The script itself worked fine before the file types changed and now I need to skip the first 23 rows of text.

I have tried two ways:

with open("D:\\Annette testing from 19 May onwards\\30cm", "*.txt",'r') as f:
    lines_after_23 = f.readlines()[23:]
    
with open("D:\\Annette testing from 19 May onwards\\30cm", "*.txt") as f:
    for line in itertools.islice(f, 24, None):  
       

but I can't get it to work... what am I doing wrong?

Original script (I need to add in the skipping of the rows to this script):

import os
import glob
from NAP_transform import transform

# %% importing and transforming all files

file_path = os.path.join("D:\\Annette testing from 19 May onwards\\30cm", "*.txt")
filenames = glob.glob(file_path)

# %% TRANSFORMING 0 DEGREE MEP NO NECK IMPACTS

# Processing each impact in the folder

for a in filenames:
     print("Now processing:", a)
# Set the name for the impact
# impact_name = a.replace("D:\\Annette testing from 19 May onwards\\", "")
# impact_name = impact_name.replace("\\", " ") # Replace slash with spaces
    impact_name = a.replace("txt", "csv") # Set as a csv file

# Transforming the data
    data_out =  transform(a, 2021)

# Setting the file path for the export files
# export_file_path = "D:\\Annette testing from 19 May onwards\\" + impact_name

# Creating and saving the full data file
    data_out.to_csv(impact_name)
ano
  • 27
  • 2
asw107
  • 1

1 Answers1

0
# test.txt is just the numbers 1-30, one per line
with open('test.txt', 'r') as f:
    # start reading from the 24th line
    for line in f.readlines()[23:]:
        print(line, end='')

# 24
# 25
# 26
etc.

The issue with with open("D:\Annette testing from 19 May onwards\30cm", "*.txt",'r') as f: lines_after_23 = f.readlines()[23:] is the "D:\Annette testing from 19 May onwards\30cm", "*.txt",'r' part- open takes the following parameters:

open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)

But you have passed two filenames (I'm assuming you were trying to read every text file within the "30cm" folder?)

In that case, what you want is this:

import os

for file in os.listdir("D:\Annette testing from 19 May onwards\30cm"):
    if file.endswith(".txt"):
        with open(file, 'r') as f:
            for line in f.readlines()[23:]:
                # do something with the line
                print(line, end='')
Mark
  • 7,785
  • 2
  • 14
  • 34