-1

I have a Javascript file Commodity.js like this:

commodityInfo = [
["GLASS ITEM", 1.0, 1.0, ], 
["HOUSEHOLD GOODS", 3.0, 2.0, ], 
["FROZEN PRODUCTS", 1.0, 3.0, ], 
["BEDDING", 1.0, 4.0, ], 
["PERFUME", 1.0, 5.0, ], 
["HARDWARE", 5.0, 6.0, ], 
["CURTAIN", 1.0, 7.0, ], 
["CLOTHING", 24.0, 8.0, ], 
["ELECTRICAL ITEMS", 1.0, 9.0, ], 
["PLUMBING MATERIAL", 1.0, 10.0, ], 
["FLOWER", 7.0, 11.0, ], 
["PROCESSED FOODS.", 1.0, 12.0, ], 
["TILES", 1.0, 13.0, ], 
["ELECTRICAL", 9.0, 14.0, ], 
["PLUMBING", 1.0, 15.0, ]
];

I want to iterate through each of the item like GLASS ITEM, HOUSEHOLD GOODS, FROZEN PRODUCTS and use the number beside it for some calculations using python. Can someone tell me how to open and iterate through the items like that in python.

Thanking You.

2
  • The OP has a JavaScript file. Commented Aug 17, 2017 at 11:10
  • @randomir That was added in after I marked it closed. I assumed OP meant JSON data. Commented Aug 17, 2017 at 11:12

2 Answers 2

1

The following code may not be the most efficient, but it works for your case.

What I'm doing here: turn the string (the content of the file) into valid JSON and then load the JSON string into a Python variable.

Note: It would be easier if the content of your JS file was already valid JSON!

import re
import json

# for the sake of this code, we will assume you can successfully load the content of your JS file
# into a variable called "file_content"
# E.G. with the following code:
#
# with open('Commodity.js', 'r') as f: #open the file
#     file_content = f.read()

# since I do not have such a file, I will fill the variable "manually", based on your sample data
file_content = """
commodityInfo = [
["GLASS ITEM", 1.0, 1.0, ],
["HOUSEHOLD GOODS", 3.0, 2.0, ],
["FROZEN PRODUCTS", 1.0, 3.0, ],
["BEDDING", 1.0, 4.0, ],
["PERFUME", 1.0, 5.0, ],
["HARDWARE", 5.0, 6.0, ],
["CURTAIN", 1.0, 7.0, ],
["CLOTHING", 24.0, 8.0, ],
["ELECTRICAL ITEMS", 1.0, 9.0, ],
["PLUMBING MATERIAL", 1.0, 10.0, ],
["FLOWER", 7.0, 11.0, ],
["PROCESSED FOODS.", 1.0, 12.0, ],
["TILES", 1.0, 13.0, ],
["ELECTRICAL", 9.0, 14.0, ],
["PLUMBING", 1.0, 15.0, ]
];
"""

# get rid of leading/trailing line breaks
file_content = file_content.strip()

# get rid of "commodityInfo = " and the ";" and make the array valid JSON
r = re.match(".*=", file_content)
json_str = file_content.replace(r.group(), "").replace(";", "").replace(", ]", "]")

# now we can load the JSON into a Python variable
# in this case, it will be a list of lists, just as the source is an array of array
l = json.loads(json_str)

# now we can do whatever we want with the list, e.g. iterate it
for item in l:
    print(item)
Sign up to request clarification or add additional context in comments.

Comments

-1

You can use for loops to achieve that.

Something like this would work:

for commodity in commodityInfo:
    commodity[0] # the first element (e.g: GLASS ITEM)
    commodity[1] # the second element (e.g: 1.0)
    print(commodity[1] + commodity[2]) #calculate two values

You can learn more about for loops here

3 Comments

Can explain the downvote ? as the question mentioned Can someone tell me how to open and iterate through the items like that in python. It states python.
Can you tell me how to open the file as the list is in a different file names CommodityDB.js . PS - I did not downvote your comment.
@ArnabC You may refer to the first solution of this stackoverflow.com/questions/38622385/… . It may be helpful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.