Skip first couple of lines while reading lines in Python file
Use a slice, like below:
with open('yourfile.txt') as f:
lines_after_17 = f.readlines()[17:]
If the file is too big to load in memory:
with open('yourfile.txt') as f:
for _ in range(17):
next(f)
for line in f:
# do stuff
how to skip certain line in text file and keep reading the next line in python?
It's not necessary to add an if else
statement in for
loop, so you can modify your code in this way:
f = open("test.txt", "r")
word = "man"
for line in f:
if not word in line:
print line
Furthermore, the issue in your code is that you are using f.next()
directly in a for loop used to scan the file. This is the reason because when the line contains "man" word, your code skips two lines.
If you want preserve if else
statement because this is only an example of a more complex problem, you can use the following code:
f = open("test.txt", "r")
word = "man"
for line in f:
if word in line:
continue
else:
print line
Using continue
, you skip one loop's iteration, and so you can reach your goal.
As Alex Fung suggests, would be better use with
, so your code would become like this:
with open("test.txt", "r") as test_file:
for line in test_file:
if "man" not in line:
print line
Want to skip last and first 5 lines while reading file in python
You could use a neat feature of slicing, you can count from the end with negative slice index, (see also this question):
lines = f.readlines()[5:-5]
just make sure there are more than 10 lines:
all_lines = f.readlines()
lines = [] if len(all_lines) <= 10 else all_lines[5:-5]
(this is called a ternary operator)
How to skip 2 lines in a file with Python?
I usually use next()
when I want to skip a single line, usually a header for a file.
with open(file_path) as f:
next(f) # skip 1 line
next(f) # skip another one.
for line in f:
pass # now you can keep reading as if there was no first or second line.
Note: In Python 2.6 or earlier you must use f.next()
python pandas read text file, skip particular lines
Here is an attempt to 'craft magic'. The idea is to try read_csv
with different skiprows
until it works
import pandas as pd
from io import StringIO
data = StringIO(
'''
=========================================
hello 123
=========================================
Dir: /x/y/z/RTchoice/release001/data
Date: 17-Mar-2020 10:0:08
Output File: /a/b/c/filename.txt
N: 2842
-----------------------------------------
Subject col1 col2 col3
001 10.00000 1.00000 3.00000
002 11.00000 2.00000 4.00000
''')
for n in range(1000):
try:
data.seek(0)
df = pd.read_csv(data, delimiter = "\s+", skiprows=n)
except:
print(f'skiprows = {n} failed (exception)')
else:
if len(df.columns) == 1: # do not let it get away with a single-column df
print(f'skiprows = {n} failed (single column)')
else:
break
print('\n', df)
output:
skiprows = 0 failed (exception)
skiprows = 1 failed (exception)
skiprows = 2 failed (exception)
skiprows = 3 failed (exception)
skiprows = 4 failed (exception)
skiprows = 5 failed (exception)
skiprows = 6 failed (exception)
skiprows = 7 failed (exception)
skiprows = 8 failed (single column)
Subject col1 col2 col3
0 1 10.0 1.0 3.0
1 2 11.0 2.0 4.0
Related Topics
Using Look Up Tables in Python
Numpy Array Typeerror: Only Integer Scalar Arrays Can Be Converted to a Scalar Index
Regex to Find Words Between Two Tags
How to Remove Zeros After Decimal from String Remove All Zero After Dot
How to Clear All Variables in the Middle of a Python Script
How to Merge Columns from Multiple CSV Files Using Python
Pytest Cannot Import Module While Python Can
Keep Other Columns When Doing Groupby
Pyspark Add New Row to Dataframe
What Is the Simplest Way to Ssh Using Python
How to Append a List Withoud Adding the Quote
Save Variables in Every Iteration of for Loop and Load Them Later
Python: How to Find the First Day of Every Month Between Two Date Ranges
How to Extract Column Value Within Square Brackets in Pyspark
Python, Delete Json Element Having Specific Key from a Loop
Regular Expression: Match Everything After a Particular Word