使用批处理脚本从csv文件中获取特定数据

问题描述 投票:0回答:1

我有文本文件'data1.txt'。它包含大量的数据,例如以下格式:

["10","20"],"comingSoonDate":null},{"stockNumber":18517314,"vin":"4A32B3FF7CE013493","year":2012,"make":"Mitsubishi","model":"Galant","body":"4DSedan","trim":"SE","basePrice":10599.0,"msrp":21900.0,"mileage":63908,"storeId":6072,"storeName":"Capitol Expressway","storeCity":"San Jose","geoCity":"San Jose","state"

如何提取“ vin号”,这是动态编号和型号名称。我想收集存储在data1.txt文件中的所有vin号和型号。提取数据后,它将存储到csv文件中。请任何人可以帮助我吗?任何批处理代码将不胜感激。

javascript java c# python batch-file
1个回答
0
投票

对,所以您要处理的是JSON

{
    "value_mapped": [ { "key": 123 }, { "key": 234 } ]
}

将假定最基本的示例,但基本上保持不变

import json

with open("file.txt", "r") as j_file:
    data = json.load(j_file)

array_data = data["value"] # this is [ {"key": 123}, ... ]
for obj in array_data:
    print(obj["key"]) # this prints 123 first iteration ans 234 second iteration

现在将其应用于您的特定情况,对于CSV,我建议使用csv模块https://docs.python.org/3/library/csv.html

© www.soinside.com 2019 - 2024. All rights reserved.