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

Tic-Tac-Toe in JavaScript

Tic-Tac-Toe in JavaScript

To take a break from the other projects this weekend I worked on a Tic-Tac-Toe game in JavaScript. The basic instructions I had for myself was it needed to be able to be played by 1 or 2 people. I started with creating the game for 2 players. Thinking that if I can...

read more
Save posts to a WordPress Database – Working Notes

Save posts to a WordPress Database – Working Notes

This is notes/overview of a project I started on to add several posts to WordPress (WP) from HTML files without having to copy and paste each file. These are just notes from that dropped project and the basic Python script to do this (github). Make sure you fully test...

read more
CMYK converter added

CMYK converter added

CMYK is a tricky one, since each device would interpret the colors differently. Had almost decided not to even include it but thought it could be useful at times. Looking around I found a basic formula that most people were using to give a rough idea for the color. I...

read more

Pin It on Pinterest

Share This