【问题标题】:Iterate through Javascript variable using Python使用 Python 遍历 Javascript 变量
【发布时间】:2017-08-17 19:34:13
【问题描述】:

我有一个像这样的 Javascript 文件 Commodity.js

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, ]
];

我想遍历每个项目,如玻璃物品、家居用品、冷冻产品,并使用它旁边的数字使用 python 进行一些计算。 谁能告诉我如何在 python 中打开和遍历类似的项目。

谢谢你。

【问题讨论】:

  • OP 有一个 JavaScript 文件。
  • @randomir 这是在我将其标记为关闭后添加的。我以为 OP 是指 JSON 数据。

标签: javascript python python-2.7 loops


【解决方案1】:

以下代码可能不是最有效的,但它适用于您的情况。

我在这里所做的:将字符串(文件的内容)转换为有效的 JSON,然后将 JSON 字符串加载到 Python 变量中。

注意:如果你的 JS 文件的内容已经是有效的 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)

【讨论】:

    【解决方案2】:

    您可以使用for 循环来实现。

    这样的事情会起作用:

    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
    

    你可以了解更多关于for循环here

    【讨论】:

    • 可以解释下否决票吗?正如问题提到的Can someone tell me how to open and iterate through the items like that in python. 它指出python
    • 你能告诉我如何打开文件,因为列表位于不同的文件名 CommodityDB.js 中。 PS - 我没有否决你的评论。
    • @ArnabC 你可以参考这个stackoverflow.com/questions/38622385/…的第一个解决方案。这可能会有所帮助。
    猜你喜欢
    • 2019-01-07
    • 2022-12-12
    • 1970-01-01
    • 2020-01-20
    • 2018-05-26
    • 1970-01-01
    • 1970-01-01
    • 2018-10-18
    • 2016-02-05
    相关资源
    最近更新 更多