D
Deleted member 969102
Guest
So I've been writing a script for a few days that does a long list of different things. One section of it involves writing a csv with a long list of assets and the last time they were updated, turning multiple lists into a single file.
Here's the code that is writing the results to a .csv:
I repeat this while section for each different sets of data I am using, like this:
This works fine and it does the job, however, it leads to a lot of unnecessary code and if I then want to integrate another list of assets & dates I have to add a whole new section.
I was wondering if there is a way to condense my code?
So I am currently writing example_link[0] and example_lastmod[0] to the csv and then deleting [0] (This then makes [1] > [0] and repeats until the example_lastmod list is empty.) When that is done, in a new while loop, I then write example1_link[0] and example1_lastmod[0] to the end of the same file.
Would something like this work?
The above does work, however, I get 400 less results in my CSV when running it for some reason.
Here's the code that is writing the results to a .csv:
Code:
i = 1
while i < len(example_link):
with open(sheet_name, 'a') as csv_file:
writer = csv.writer(csv_file)
writer.writerow([example_link[0].text.strip(), example_lastmod[0].text.strip()])
del example_link[0]
del example_lastmod[0]
I repeat this while section for each different sets of data I am using, like this:
Code:
i = 1
while i < len(example_link):
with open(sheet_name, 'a') as csv_file:
writer = csv.writer(csv_file)
writer.writerow([example_link[0].text.strip(), example_lastmod[0].text.strip()])
del example_link[0]
del example_lastmod[0]
i = 1
while i < len(example1_link):
with open(sheet_name, 'a') as csv_file:
writer = csv.writer(csv_file)
writer.writerow([example1_link[0].text.strip(), example1_lastmod[0].text.strip()])
del example1_link[0]
del example1_lastmod[0]
i = 1
while i < len(example2_link):
with open(sheet_name, 'a') as csv_file:
writer = csv.writer(csv_file)
writer.writerow([example2_link[0].text.strip(), example2_lastmod[0].text.strip()])
del example2_link[0]
del example2_lastmod[0]
This works fine and it does the job, however, it leads to a lot of unnecessary code and if I then want to integrate another list of assets & dates I have to add a whole new section.
I was wondering if there is a way to condense my code?
So I am currently writing example_link[0] and example_lastmod[0] to the csv and then deleting [0] (This then makes [1] > [0] and repeats until the example_lastmod list is empty.) When that is done, in a new while loop, I then write example1_link[0] and example1_lastmod[0] to the end of the same file.
Would something like this work?
Code:
links = [example, example1, example2]
lastmod = [example_l, example1_l, example2_l]
i = 1
while i < len(links[0]):
link_a = links[0]
lastmod_a = lastmod[0]
with open(sheet_name, 'a') as csv_file:
writer = csv.writer(csv_file)
writer.writerow([link_a[0].text.strip(), lastmod_a[0].text.strip()])
del link_a[0]
del lastmod_a[0]
if len(link_a) == 1:
del links[0]
del lasmod[0]
The above does work, however, I get 400 less results in my CSV when running it for some reason.
Last edited by a moderator: