Touching Pandas and XLSXWriter

·

2 min read

Code written by a colleague has been getting a 'make better' touch from me.

The code

Downloaded a table from a webpage into a spreadsheet file. The file had a generic name that the code renamed to add in one additional piece of information, page name.

Adding a datetime stamp to the file name allowed the code to run more than once without erroring out from duplicate file names.
Creating a folder with the name of today's date, with some error management, redirects the file save into that folder gives better document management.

A counter variable was being used to determine which page url to action. That is okay if the list doesn't change and counter == 0 and item[0] are always in sync. I removed the counter and changed all of the if/elif statements to expect the string of the item rather than a number. Thus ensuring if the list alters then the correct page url would still be actioned.

So now we have the expected page downloaded to a file each time.

Looking at the content of the file I found there were no column headers. Using Pandas to bring in the file as a dataframe, create a xlsxwriter instance and save the file.
Oh and I renamed the sheet with the pagename rather than 'Sheet 1'.

This is for a future feature of adding all the page extracts into one spreadsheet file.