Please excuse my poor English.
I have this code here that writes a pdf using reportlab. I am picking the data from two CSV files. For the second file, if it's not available, an optional file is provided. When I run it in the iterable, some of the pages do not come out. I know this is because the first file and the second file are of the same length but the first file and the second file will not be of the same length if the except part is executed. Is there a way I can code it so that if the file1 and file2(except) are what are available then the length of file2(except can adjust to the same level as file1 so that it iterates through all the items in file1.
file1 = pd.read_csv('file1.csv')
try:
file2 = pd.read_csv('file2.csv')
except:
file2 = pd.read_csv('option2_file.csv')
c = canvas.Canvas('samplepdf.pdf')
for i, x in zip(file1,file2):
a = i[0]
b = i[1]
c = i[2]
d = i[4]
e = x[0]
f = x[1]
'''
Do something the values
'''
c.save()
Whatever I have up here might not be easy to understand. What I want is to iterate through all items in file1 but it isn't happening because file2 is of different length of items. Wondering if I can find a way to write a code to adjust the length of file2 to that of file1 so that the process succeeds. File2 is just an empty file with some __ I keyed in.
Python3 has a function called itertools.zip_longest. From the description, it may do exactly what you want. All you have to provide is a default fill value for the elements that are missing from the shorter of the two files.