skipping a line while reading a file with a for loop - python

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...

Related

Read files of a line in 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.

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

Writing to two specific positions in each line of a text file (or rather creating a new file with the information added)

aspiring Python newb (2 months) here. I am trying to create a program that inserts information to two specific places of each line of a .txt file, actually creating a new file in the process.
The information in the source file is something like this:
1,340.959,859.210,0.0010,VV53
18abc,34099.9590,85989.2100,0.0010,VV53
00y46646464,34.10,859487.2970,11.4210,RP27
Output would be:
1,7340.959,65859.210,0.0010,VV53
18abc,734099.9590,6585989.2100,0.0010,VV53
00y46646464,734.10,65859487.2970,11.4210,RP27
Each line different, hundreds of lines. The specific markers I'm looking for are the first and second occurence of a comma (,). The stuff needs to be added after the first and second comma. You'll know what I mean when you see the code.
I have gotten as far as this: the program finds the correct places and inserts what I need, but doesn't write more than 1 line to the new file. I tried debugging and seeing what's going on 'under the hood', all seemed good there.
Lots of scrapping code and chin-holding later I'm still stuck where I was a week ago.
tl;dr Code only outputs 1 line to new file, need hundreds.
f = open('test.txt', 'r')
new = open('new.txt', 'w')
first = ['7']
second = ['65']
line = f.readline()
templist = list(line)
counter = 0
while line != '':
for i, j in enumerate(templist):
if j == ',':
place = i + 1
templist1 = templist[:place]
templist2 = templist[place:]
counter += 1
if counter == 1:
for i, j in enumerate(templist2):
if j == ',':
place = i + 1
templist3 = templist2[:place]
templist4 = templist2[place:]
templist5 = templist1 + first + templist3 + second + templist4
templist6 = ''.join(templist5)
new.write(templist6)
counter += 1
break
if counter == 2:
break
break
line = f.readline()
templist = list(line)
f.close()
new.close()
If I'm understanding your samples and code correctly, this might be a valid approach:
with open('test.txt', 'r') as infd, open('new.txt', 'w') as outfd:
for line in infd:
fields = line.split(',')
fields[1] = '7' + fields[1]
fields[2] = '65' + fields[2]
outfd.write('{}\n'.format(','.join(fields)))

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
...

How to count the number of times a digit appears at the beginning of each number in a file? (python)

I am trying to count the number of times 1,2,3,...,9 appear at the beginning of each number in a file. This is how my code goes:
DECIMAL_NUM='123456789'
def main():
#get the file name from the user
file_name=str(input("Enter a file name: "))
#open the file to read
input_file= open(str(file_name),'r')
#reads the first line of the file
line=input_file.readline().strip()
one=0
two=0
three=0
four=0
five=0
six=0
seven=0
eight=0
nine=0
i=0
while line!="":
if line[0]==DECIMAL_NUM[0]:
one+=1
elif line[0]==DECIMAL_NUM[1]:
two+=1
elif line[0]==DECIMAL_NUM[2]:
three+=1
elif line[0]==DECIMAL_NUM[3]:
four+=1
elif line[0]==DECIMAL_NUM[4]:
five+=1
elif line[0]==DECIMAL_NUM[5]:
six+=1
elif line[0]==DECIMAL_NUM[6]:
seven+=1
elif line[0]==DECIMAL_NUM[7]:
eight+=1
elif line[0]==DECIMAL_NUM[8]:
nine+=1
line=input_file.readline().strip()
i+=1
input_file.close()
print(one)
print(two)
main()
I am also counting how many numbers are there in the file, so that I can calculate percentage of appearance of each digit. I think my codes are a little bit wordy and there might be a better way to do it. The input file has the following numbers:
1292
1076
188040
1579
3510
2597
3783
64690
For some reason, I am getting the number of times 1 is appearing as 1, when it should be 5. Could someone please give me some pointers? Thanks
Here is one way of approaching this task:
# Get non-empty lines from input file:
relevant_lines = [line for line in open(file_name).readlines() if line.strip()]
# Count them:
num_lines = len(relevant_lines)
import defaultdict
# If a key does not exist in a defaultdict when adding a value for it,
# it will be added with a default value for the given data type
# (0 in case of int):
d = defaultdict(int)
# Iterate through lines; get first character of line
# and increment counter for this character by one in defaultdict:
for line in relevant_lines:
d[line[0]] += 1
# Print results:
for key, value in d.items():
print(k + ' appears ' + value + ' times in file.')
If you are not allowed to use dicts, here's how to fix your code:
DECIMAL_NUM='123456789'
def main():
# Get file name from user
file_name = input("Enter a file name: ")
# Open the file to read, and get a list of all lines:
lines = open(file_name, 'r').readlines()
one = 0
two = 0
three = 0
four = 0
five = 0
six = 0
seven = 0
eight = 0
nine = 0
for line in lines:
if line.strip(): # Check if line is not empty
if line[0] == DECIMAL_NUM[0]:
one += 1
elif line[0] == DECIMAL_NUM[1]:
two += 1
elif line[0] == DECIMAL_NUM[2]:
three += 1
elif line[0] == DECIMAL_NUM[3]:
four += 1
elif line[0] == DECIMAL_NUM[4]:
five += 1
elif line[0] == DECIMAL_NUM[5]:
six += 1
elif line[0] == DECIMAL_NUM[6]:
seven += 1
elif line[0] == DECIMAL_NUM[7]:
eight += 1
elif line[0] == DECIMAL_NUM[8]:
nine += 1
print(one)
print(two)
main()
You code is fine. It's your data file that's giving you problem. Remove the blank lines and your program should give you the right results.
1292
1076
188040
1579
3510
2597
3783
64690
After you processed the first line, the next line is read. But that's a blank line and your while loop ends.

Categories

Resources