Inventory HTML page to a CSV file

January 07, 2023

As a New Year’s resolution, not mine, they wanted to make sure their online store, reseller site, inventory was up to date. They wanted to make sure their inventory was cleaned up and make sure that what’s listed on the site matches their own inventory. They had been doing this for several years, with different people helping out at times; this isn’t there full-time job.

They needed a spreadsheet with the important info of their inventory, they couldn’t find a way to get a spreadsheet from their online reseller location and asked me late on Dec. 31 if I could create one from the HTML files they downloaded by Jan 3. This only gave me 2 days along with another New Year’s resolution project for somebody else to get done.

The request:

The spreadsheet needed to show:
The item name; Quantity; SKU #; Status
Needed to be open in Excel
Had to get it done within 2 days

This project is more about turning code around quickly, a lot of the time you don’t have a lot of time to create the program, you just need to get it done. In cases like that you work to get the code to work then if you have time go back and clean it up as much as you can.

When planning this out the first thought was, I had started to play with Selenium. This would cause too many issues I would need their password, then if there’s a 2nd verification system has to work with them to get that through. With how little they might be using this, the site might change so much by the next time that it would need rewritten and tested again, where just using the HTML files and update code, if needed, would be a faster approach.

First I decided to create a CSV file, Excel will open this file, and used some of the code I had already wrote for the gathering email addresses program:
https://www.lynnamacher.com/python-pull-email-addresses-to-csv-file/
This way I would have the basic structure setup for creating the CSV file right from the beginning.

Looking through the code I found that the elements ‘Item name’, ‘Quantity’ and ‘SKU’ where all on the same line. That made getting those easier then set the trigger for the line and pulled each element out into its separate variables. The ‘Status’ was off on its own line so had to check for that separately; this could have caused issues if these ‘Status’ words were used at the beginning of other lines.

The first big hurdle that happens when writing these to the CSV file it converted them into tuples with each blank elements so I would get 2 tuples like this: (‘Item Name’, ‘Quantity’, ‘SKU’, ‘’, ‘’) (‘’,  ’’, ’’, ’Status’). I worked with this for a time then converted the saves to add them to a list and then process the list when done. I might have been able to run over the tuples checking for non-null areas then pull those to write. This won’t work very well if we were looking at hundreds of pages to process, but for these few it was fine.

When processing the list, I pulled the items out and increased the count by 1 at the end of each element to help keep track quicker. If there would have been times where there could be empty values passed, I could have looked as lists inside the list, or pass a blank (‘’). Then wrote the line and pulled the next from the list that has already been counted out. There’s a lot I could have cleaned up here, but it’s easy to read and a simple enough program to read through.

In the end I was able to get both projects done before Tuesday, Jan. 3. While it might not be the best written or cleanest code, it’s working code. For simple one-time projects this is what is needed, the clients won’t want to pay you for hours of making it clean and showing off how you can have 20 lines of code written in 1 line; mostly they won’t care how it looks for a simple one-time project as long as it works. If running on a server, part of a bigger application or for another programmer which this isn’t, then having code cleaned up and able to be processed fast using as few resources is preferred, where spending the extra time is needed.

Git: https://github.com/amacher/inventoryCSV/blob/main/Inv_GIT.py

Related Articles

Python – Birthday List

Python – Birthday List

Birthday List Python Program Worked on a project for a publication to pull the birthdays of their members quickly. To help demonstrate what they were working with; they had two Word files containing tables of birthday months, the dates for the month were not in order...

read more
Microsoft Certified: Azure AI Fundamentals

Microsoft Certified: Azure AI Fundamentals

Passed the Microsoft Certified: Azure AI Fundamentals Certification:https://learn.microsoft.com/api/credentials/share/en-us/LynnAmacher-1690/4DB4563B79AA3C11?sharingId=9196330BBB36EAF4 The graphic was created in Microsoft Image creator with DALL-E where I added the...

read more
AI as your assistant

AI as your assistant

With all the talk of AI will be taking your job and articles like you don’t need to learn coding AI will do it all for you. I’ve been playing around with AI, decided to try to see how much of what AI can be done for me. I use AI generical throughout the post and not...

read more

Pin It on Pinterest

Share This