Read files of a line in python - python

I have a 1000 files named coord_1.xvg, coord_2.xvg and so on. I have written a program that reads the second column of each of these files and gives me the output on the if else condition that I have provided. It prints a list of N's followed by either P or R depending on whichever is obtained first and stops.
from pathlib import Path
for file in Path("/home/abc/xyz/coord/").rglob("*.xvg"):
with file.open("r") as f:
for i, line in enumerate(file):
if i < 22:
continue
line = line.strip().split()
if float(line[1]) >= 9.5:
print("P")
break
elif float(line[1]) <= 5.9:
print("R")
break
else:
print("N")
I want to read these files and print the value in the first column corresponding to the output P or R as the output along with P or R. Basically, I need to find the line at which the reading of this file had stopped and extract the value corresponding to it.

Create a variable outside the loop, that you can set to i when your condition is met.
from pathlib import Path
stopped = 0
for file in Path("/home/abc/xyz/coord/").rglob("*.xvg"):
with file.open("r") as f:
for i, line in enumerate(file):
if i < 22:
continue
line = line.strip().split()
if float(line[1]) >= 9.5:
print("P")
stopped = i
break
elif float(line[1]) <= 5.9:
print("R")
stopped = i
break
else:
print("N")
Also, if I am understanding correctly, your break statements should be indented to be inside the if and elif blocks
Let me know if you have any questions.

Related

How can I keep file pointer in the If clause

Could anybody advice me if there is any way to keep file pointer in the nested IF clause.
I have to parse the file, and based on its content, different code blocks should process the file.
I came up with nested IF loops.
The code:
import re
with open('smartctl.txt', 'r') as file:
line = file.readlines()
for x in line:
matchIP = re.search('Checking', x)
if matchIP:
print(x)
Match_Micron55 = re.search('Micron_5100', x)
Match_Intel = re.search('INTEL', x)
Match_Micron600 = re.search('Micron_M600', x)
Any_Micron = re.search('Micron', x)
if Match_Micron55:
print("here we have Micron55")
elif Match_Intel:
print("here we have Intel")
elif Match_Micron600:
print('here we have Micron 600')
mline = line
print("file is open")
check = ""
for y in mline:
if y == x:
check == True
continue
if y.startswith(' 1 Raw_Read_Error_Rate') and check == True:
print(y)
continue
if y.startswith(' 5 Reallocated_Sector_Ct') and check == True:
print(y)
continue
if y.startswith('173 Unknown_Attribute') and check == True:
print(y)
break
elif Any_Micron:
print('Here we have unsorted values')
As you can see I read the file as line.
Then I go with variable X through the file.
Then, when I fall in IF condition, I have to CONTINUE reading the file: that's keep the file pointer and continue reading exactly from the place I went into the IF loop. I use 2 loops here with X and Y variables (for x in line and for y in mline). Could you tell me please if I can continue reading the same file in the second(nested) If confidition?
The method seems to be non working. Apart from creating Y variable I have also tried using X in the nested IF clause but was not succeed. The x (line) variable seems to not keep its value on the second IF entry.
I suspect your problem lies in the following code:
line = file.readlines()
for x in line:
The fileread is returning a line from the file, the x in line is iterating through the line a character at a time. I bvelieve you should restructure your code as follows:
replace:
with open('smartctl.txt', 'r') as file:
line = file.readlines()
for x in line:
with:
with open('smartctl.txt', 'r') as file:
for x in file.readlines():

How to read text files in python with specified condition?

I have a text file and I want to extract the number of the line that contains certain phrases (ATOMIC_POSITIONS (angstrom) and K_POINTS (automatic)).
n = -1
with open(filename) as f:
for line in f:
n += 1
if line == "ATOMIC_POSITIONS (angstrom)":
print('test1')
start = n
elif line == "K_POINTS (automatic)":
print('test2')
end = n
print(start, end)
My problem is that python does not go inside the if statements (i.e. test1 and test2 are not printed).
But I am sure that filename contain the phrases, this is small part of filename:
0.000000613 0.000000613 1.022009120
ATOMIC_POSITIONS (angstrom)
C 1.696797551 1.714436737 -0.068349117
Simply put: your condition is not met. "==" checks for equality, which for several reasons may not be true in your case (see comments).
When checking for a string in a line of a file I would try this:
n=-1
with open(filename) as f:
for line in f:
n += 1
if "ATOMIC_POSITIONS (angstrom)" in line:
print('test1')
start = n

Break program if file is empty

The code below picks a random word from a file and later delete the word and its working great. I want the program to BREAK when file is empty since I delete every random word picked from the file.
Here is Random part of the code:
import random
import os
g = r"C:\Users\Homer\name.txt"
lines = [line.rstrip('\n') for line in open(g)]
rand = random.choice(lines)
print(rand)
Is it good to say Break if file is empty and print "file is empty"?
Or if Random (rand) return no word, then break and print file is empty.
I have already checked this site and there are some answers on how to check if a file is empty and print file empty or file size is zero or there is not word in file. However, I could find one that say BREAK program if file is empty. I get the error below when my file is empty.
--->rand = random.choice(lines)
IndexError: Cannot choose from an empty sequence
At the moment I'm using the code below to check if file is empty or not. but i keep getting error with the break statement.
if os.path.getsize("name.txt") == 0:
print("File is Empty")
else:
print("File is not Empty")
break
----> SyntaxError: 'break' outside loop
What your code should be (I think):
if os.path.getsize("name.txt") == 0:
print("File is Empty")
else:
return print("File is not Empty")
You're nearly there.
break is used to prematurely stop a for loop. The default behaviour of a loop without explicit break keywords is continue until there are no indexes left in the iterator.
You're not using a loop here. So when you ask Python to break there is nothing for it to stop.
Example usage of break keyword:
for x in range(20):
if x != 13:
print(x)
continue # uneccesary as it will continue by default
else:
print("we don't like 13")
break

if next(item) moves to the next item in a list, what is the eqvilant of next next(item) python

heres the code for context.
def processScores( file, score):
#opens file using with method, reads each line with a for loop. If content in line
#agrees with parameters in elif statements, executes code in if statment. Otherwise, ignores line
with open(file,'r') as f:
for line in f: #starts for loop for all if statements
line = line.strip()
if line.isdigit():
start = int(line)
score.initialScore(start)
print(line)#DEBUG TEST**** #checks if first line is a number if it is adds it to intial score
elif len(line) == 0:
print(line)#DEBUG TEST****
continue #if a line has nothing in it. skip it
elif line == 'o' or line == 'O':
amount = next(f)
print(line)#DEBUG TEST****
score.updateOne(amount) #if line contains single score marker, Takes content in next line and
#inserts it into updateOne
elif line == 'm'or line == 'M':
scoreList = next(f)
lst = []
for item in scoreList:
print(line)#DEBUG TEST****
lst.append(item)
score.updateMany(lst) # if line contains list score marker, creates scoreList variable and places the next line into that variable
# creates lst variable and sets it to an empty list
# goes through the next line with the for loop and appends each item in the next line to the empty list
# then inserts newly populated lst into updateMany
elif line == 'X':
print(line)#DEBUG TEST****
score.get(self)
score.average(self) # if line contains terminator marker. prints total score and the average of the scores.
# because the file was opened with the 'with' method. the file closes after
the idea that I am trying to is work with a file that looks like this:
50
O
30
O
40
M
10 20 30
o
5
m
1 2 3
X
if the code sees an 'O' or an 'o' then it needs to take the next line in the code and add it to a running score.. However the next line is a blank space... So I need to skip to the second line after the 'O' or 'o'.
I was thinking of doing an exception for this, but before I go down that road I wanna see if anyone might know of a better way.
If you want to move along f skipping whitespace-only items,
while True:
x = next(f).strip()
if x: break
will work, as will
for x in f:
x = x.strip()
if x: break
The difference is, what if there is no following non-all-space item in f. The former will exit with a StopIteration exception, the latter exit the for loop with no exception but x set to ''. Pick your poison (which exit form would you rather deal with) and code accordingly!
How about something like:
For line in lines:
if type(line) == 'int':
oneCount += line
elif type(line) == 'list':
manyCount.append(line)
elif type(line) == 'str' and line != 'x':
continue
elif type(line) == None:
continue
else:
print scores
A useful model to think about this problem is a state machine.
The code has 3 states:
Read command code.
Add single score (on "O").
Add multiple scores (on "M").
By keeping a variable with the current state, you can process the input without skipping ahead.
Now, empty lines appear to serve no purpose so you could just remove them all from the input like this:
...
non_empty_lines = (line for line in f if line.strip())
for line in non_empty_lines:
... do your thing ...
The generator expression will filter lines that are all spaces.
If for some reason you cannot use generator expressions, then do it inside the loop:
...
for line in f:
if not line.strip():
continue
...

skipping a line while reading a file with a for loop

I am trying to figure out a way to skip the next two lines in a file if a condition in the first line is true. Any ideas on a good way to do this? Here's what I have so far...
def main():
file = open(r'C:\Users\test\Desktop\test2.txt', 'r+')
ctr = 1
for current_line in file:
assert ctr<3
if current_line[0:6] == str("001IU"):
pass
else:
if ctr == 1 and current_line[9:11] == str("00"):
do something...
ctr += 1
elif ctr == 1 and current_line[9:11] != str("00"):
pass #I want it to skip the next two lines in the loop
elif ctr == 2:
do something...
ctr = 1
else:
raise ValueError
In Python 2.6 or above, use
next(file)
next(file)
to skip two items of the iterator file, i.e. the next two lines.
file.next()
file.next()
i'd do this way...

Categories

Resources