Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8800943
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:43:14+00:00 2026-06-14T00:43:14+00:00

I am writing a script that looks through my inventory, compares it with a

  • 0

I am writing a script that looks through my inventory, compares it with a master list of all possible inventory items, and tells me what items I am missing. My goal is a .csv file where the first column contains a unique key integer and then the remaining several columns would have data related to that key. For example, a three row snippet of my end-goal .csv file might look like this:

100001,apple,fruit,medium,12,red
100002,carrot,vegetable,medium,10,orange
100005,radish,vegetable,small,10,red

The data for this is being drawn from a couple sources. 1st, a query to an API server gives me a list of keys for items that are in inventory. 2nd, I read in a .csv file into a dict that matches keys with item name for all possible keys. A snippet of the first 5 rows of this .csv file might look like this:

100001,apple
100002,carrot
100003,pear
100004,banana
100005,radish

Note how any key in my list of inventory will be found in this two column .csv file that gives all keys and their corresponding item name and this list minus my inventory on hand yields what I’m looking for (which is the inventory I need to get).

So far I can get a .csv file that contains just the keys and item names for the items that I don’t have in inventory. Give a list of inventory on hand like this:

100003,100004

A snippet of my resulting .csv file looks like this:

100001,apple
100002,carrot
100005,radish

This means that I have pear and banana in inventory (so they are not in this .csv file.)

To get this I have a function to get an item name when given an item id that looks like this:

def getNames(id_to_name, ids):
    return [id_to_name[id] for id in ids]

Then a function which gives a list of keys as integers from my inventory server API call that returns a list and I’ve run this function like this:

invlist = ServerApiCallFunction(AppropriateInfo)

A third function takes this invlist as its input and returns a dict of keys (the item id) and names for the items I don’t have. It also writes the information of this dict to a .csv file. I am using the set1 – set2 method to do this. It looks like this:

def InventoryNumbers(inventory):
    with open(csvfile,'w') as c:
        c.write('InvName' + ',InvID' + '\n')
    missinginvnames = []
    with open("KeyAndItemNameTwoColumns.csv","rb") as fp:
        reader = csv.reader(fp, skipinitialspace=True)
        fp.readline() # skip header
        invidsandnames = {int(id): str.upper(name) for id, name in reader}
    invids = set(invidsandnames.keys())
    invnames = set(invidsandnames.values())
    invonhandset = set(inventory)
    missinginvidsset = invids - invonhandset
    missinginvids = list(missinginvidsset)
    missinginvnames = getNames(invidsandnames, missinginvids)
    missinginvnameswithids = dict(zip(missinginvnames, missinginvids))
    print missinginvnameswithids
    with open(csvfile,'a') as c:
        for invname, invid in missinginvnameswithids.iteritems():
            c.write(invname + ',' + str(invid) + '\n')

    return missinginvnameswithids

Which I then call like this:

InventoryNumbers(invlist)

With that explanation, now on to my question here. I want to expand the data in this output .csv file by adding in additional columns. The data for this would be drawn from another .csv file, a snippet of which would look like this:

100001,fruit,medium,12,red
100002,vegetable,medium,10,orange
100003,fruit,medium,14,green
100004,fruit,medium,12,yellow
100005,vegetable,small,10,red

Note how this does not contain the item name (so I have to pull that from a different .csv file that just has the two columns of key and item name) but it does use the same keys. I am looking for a way to bring in this extra information so that my final .csv file will not just tell me the keys (which are item ids) and item names for the items I don’t have in stock but it will also have columns for type, size, number, and color.

One option I’ve looked at is the defaultdict piece from collections, but I’m not sure if this is the best way to go about what I want to do. If I did use this method I’m not sure exactly how I’d call it to achieve my desired result. If some other method would be easier I’m certainly willing to try that, too.

How can I take my dict of keys and corresponding item names for items that I don’t have in inventory and add to it this extra information in such a way that I could output it all to a .csv file?

EDIT: As I typed this up it occurred to me that I might make things easier on myself by creating a new single .csv file that would have date in the form key,item name,type,size,number,color (basically just copying in the column for item name into the .csv that already has the other information for each key.) This way I would only need to draw from one .csv file rather than from two. Even if I did this, though, how would I go about making my desired .csv file based on only those keys for items not in inventory?

ANSWER: I posted another question here about how to implement the solution I accepted (becauseit was giving me a value error since my dict values were strings rather than sets to start with) and I ended up deciding that I wanted a list rather than a set (to preserve the order.) I also ended up adding the column with item names to my .csv file that had all the other data so that I only had to draw from one .csv file. That said, here is what this section of code now looks like:

MyDict = {}
infile = open('FileWithAllTheData.csv', 'r')
for line in infile.readlines():
    spl_line = line.split(',')
    if int(spl_line[0]) in missinginvids: #note that this is the list I was using as the keys for my dict which I was zipping together with a corresponding list of item names to make my dict before.
        MyDict.setdefault(int(spl_line[0]), list()).append(spl_line[1:])
print MyDict
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T00:43:15+00:00Added an answer on June 14, 2026 at 12:43 am

    it sounds like what you need is a dict mapping ints to sets, ie,

    MyDict = {100001: set([apple]), 100002: set([carrot])}

    you can add with update:

    MyDict[100001].update([fruit])

    which would give you: {100001: set([apple, fruit]), 100002: set([carrot])}

    Also if you had a list of attributes of carrot… [vegetable,orange]

    you could say MyDict[100002].update([vegetable, orange])

    and get: {100001: set([apple, fruit]), 100002: set([carrot, vegetable, orange])}

    does this answer your question?

    EDIT:

    to read into CSV…

    infile = open('MyFile.csv', 'r')
    for line in infile.readlines():
        spl_line = line.split(',')
        if int(spl_line[0]) in MyDict.keys():
            MyDict[spl_line[0]].update(spl_line[1:])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a script that'll read through my ftpd logs and generate a hash
I'm writing a script that parses the pure-ftpwho -s command to get a list
I'm writing a program that looks through CSVs in a directory and appends the
I am writing a bash script that modifies a file that looks like this:
So I'm writing a script at work where I have to go through all
I am writing a script that will look in a custom reports directory, copy
I'm writing a script that will constantly scan iTunes for new dialog boxes and
I'm writing a script that asks the user for several options and then, via
I am writing a script that checks if given domain is parked or not.
I'm writing a script that will ping my ip range. Here's what I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.