for loop over list break and continue

2024/11/16 19:26:53

To specify the problem correctly :i apologize for the confusion Having doubts with breaking early from loop . I have folders - 1995,1996 to 2014 . Each folder has xml files. In some xml files the entry - MaxAmdLetterDate is None. I want to proceed to the next file in the folder, instead right now with break , the program execution goes to the next folder and skips all files in that folder when it encounters a break This is the code

import xml.etree.ElementTree as ET
import os
for yrcnt in range(1995,2015):old= old="./old/"+ str(yrcnt)for dirpath, subdirs, files in os.walk(old): #folder name in which subfolder>files are presentprint filesfor filename in files:if filename.endswith(".xml") == True:fl=filenameprint flprint "-----Reading current file -------"  + str(fl)flnm=str(dirpath)+"/"+str(filename)tree1 = ET.parse(flnm)                        #db on systemroot1 = tree1.getroot()for ch1 in root1.findall('Award'):aid1 = ch1.find('AwardID').textmaxdate1=ch1.find('MaxAmdLetterDate').textprint "Max amd date : "+str(maxdate1)if maxdate1==None:break

Help is appreciated .

ORIGINAL POST: My for loop gets an early exit on encountering a break . Any remedies ?

files=[1,2,3,4,5]
for filename in files:print filenameif filename==3:break

o/p:

  123

expected O/p

  1245

Thanks !

EDIT: With some comments such as this is expected function of break . Let me elucidate: I want the function to be similar to a range(0,5) , Iam using this as a part of nested loop where I want the loop to go on , and not exit out due to one file .

Answer

This is the way break works. If you want to iterate over the whole list, simply remove the break (or if you want to do something with the value, use pass as a placeholder).

values = [1, 2, 3, 4, 5]
for value in values:if value == 3:passprint(value)

Output:

1
2
3
4
5  

If you just want to a skip a value, use continue keyword.

values = [1, 2, 3, 4, 5]
for value in values:if value == 3:continueprint(value)

Output:

1
2
4
5 
https://en.xdnf.cn/q/120289.html

Related Q&A

ImportError: cannot import name loads from json (unknown location)

Previos title was: AttributeError: module json has no attribute loads I changed it because it looks similar to this but at the link that i provided, the problem seems that the person was having a file…

How can I filter the domains served by a CDN from a list of domain names?

I have a list of domains and I need to filter the domains served by a CDN(Content Delivery Network). I am going to use python script to do that. At the first I was thinking I can identify them from the…

Convert int(round(time.time())) to C# [closed]

Its difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi…

How to iterate over all elements of a 2D matrix using only one loop using python

I know you can iterate over a 2d matrix using two indexes like this: import numpy as npA = np.zeros((10,10))for i in range(0,10):for j in range(0,10):if (i==j):A[i,j] = 4Is there a way of doing this us…

Parse table names from a bunch SQL statements

I have an table with thousands of SQL statements in a column called Queries. Any ideas on how to get just the table names from the statements by using a regular expression?

click multiple buttons with same class names in Python

This a column in a table this column contains buttons, on pressing each buttons a pdf is downloadedThe buttons have the same class names and I want to click on all the buttons.This is what I did, but i…

Python equivalent to subset function in r [duplicate]

This question already has answers here:subsetting a Python DataFrame(6 answers)Closed 4 years ago.I dont know python at all but the project Im currently working on must be done using it, I have this r …

Force subprocess to use Python 3 [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 5…

Python: Im making a simple calculator for class. Whats wrong with this code? [duplicate]

This question already has answers here:How can I read inputs as numbers?(10 answers)Closed 7 months ago.My teacher requests me to make a calculator that can calculate a 15% tip on a submitted price. I…

FileNotFoundError Python Script

I am trying to run a python script, .py in the windows command prompt. I drag the script from my files into the command prompt window. I run it. Then, the script presents a prompt for me to enter the f…