0

I am trying to create 3 nested for loops and skip over any values that is not in any of the for loops. I have tried different ways but have not had much success yet. Here is the for loops:

for industry in string.ascii_uppercase:
    for code in range(1, 99):
        for year in range(1998, 2019):
            createCSV(industry, code, year)

the problem is there are industry and code values for which data is not available. The code breaks at code=3 because 3 is missing in the original values for the variable code. If it was only one value I could just skip it using continue.

for industry in string.ascii_uppercase:
for code in range(1, 99):
    if code == 3:
        continue
    for year in range(1998, 2019):
        createCSV(industry, code, year)

But there more missing values for both industry and code and I am trying to find an efficient way. Thanks for your help.

josh
  • 15
  • 6
  • You probably want a `try` and `except` statement. – Jack Moody Jan 01 '19 at 20:08
  • Sounds like you clearly know what values are invalid so why not just store them in a `set` and then check containment while looping? – pstatix Jan 01 '19 at 20:12
  • Do you know ahead of time which values are valid/invalid? – Carcigenicate Jan 01 '19 at 20:15
  • @pstatix and Carciagenicate I do not know what values are missing but I can figure that out. however, it would take time and the missing list would be too long so i am looking for a more efficient way to do that. – josh Jan 01 '19 at 21:19

3 Answers3

1

One possible way could be with a try/except clause:

for industry in string.ascii_uppercase:
    for code in range(1, 99):
        for year in range(1998, 2019):
            try:
                createCSV(industry, code, year)
            except Exception:
                print(f"Warning: unable to create CSV for industry '{industry}' for code '{code}' for year '{year}'")

in which you can still replace the generic Exception with the specific exception class that your createCSV function raises. If you know for example that createCSV will raise a ValueError when an invalid code value is provided as input argument, then replace except Exception: with except ValueError:. This will help avoid some potential pitfalls (see here for more info).

However if at all possible I would recommend to instead:

  • Retrieve an iterable with only the valid codevalues beforehand and then only loop over these, OR
  • Modify the programming code of createCSV such that it doesn't break when an invalid code value is provided.
Xukrao
  • 8,003
  • 5
  • 26
  • 52
1

If you already know which values causes error then avoid looping over them.

missing_code_values=[3,4,5,6,7,83]
missing_ascii_values=['A','Z']
for industry in [x for x in string.ascii_uppercase if x not in missing_ascii_values]:
    for code in [x for x in range(1, 99) if x not in missing_code_values]:
        for year in range(1998, 2019):
            createCSV(industry, code, year)

If you don't know it, then you can use try/except

for industry in string.ascii_uppercase:
    for code in range(1, 99):
        for year in range(1998, 2019):
            try:
                createCSV(industry, code, year)
            except: # mention specific exception if you can
                print("Skipping Industry-",industry," Code-",code," Year-",year)
Bitto
  • 7,937
  • 1
  • 16
  • 38
-3

You can check against Null in Python:

 for industry in string.ascii_uppercase:
 for code in range(1, 99):
   if code is None:
     continue
 for year in range(1998, 2019):
   createCSV(industry, code, year)
Janneck Lange
  • 882
  • 2
  • 11
  • 34
  • This won't work because `code` wont be `None` when iterating the range. The OP is stating that they know which values they need to skip, not test for nullness. – pstatix Jan 01 '19 at 20:13