Python TypeError用于逻辑迭代数据值 [英] Python TypeError with for logic iterating data values

查看:97
本文介绍了Python TypeError用于逻辑迭代数据值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

循环中获取 c $ c> json.load()函数进行阅读时遇到问题闹钟字段,并带回他们的价值。

I'm having a problem getting the for loop of the json.load() function to read through the "alarmst" fields and bring back their values.

我的工作代码高于问题代码,可以使用相同的数据获取标签字段数据值正常。

I have working code above the problem code that works fine with the same data getting the "tag" field data values just fine.

我认为这可能与 dataStatus dataStart 用分号( 2015-12-10T05:59:03Z )格式化数据,所以我不知道如何解析这些值或替换值中的字符在循环之前,通过 writerow()函数,或者如果有人知道一个更好的方法来告诉它,这些特定字段的值的数据类型是日期或与Python类似的东西。

I think it may be related to the dataStatus and dataStart having time formatted data with semicolons i.e. (2015-12-10T05:59:03Z) so I'm not sure how to parse those out or replace characters in the values in the loop before running through the writerow() function, or if someone knows of a better way to tell it that the data type of the value of those specific fields is Date or something explicit like that with Python.

import json
import csv

with open('C:\\folder\\dev\\Tags.txt',"r") as file:
    data = json.load(file)

with open('C:\\folder\\dev\\Tags.csv',"w",newline='') as file:

    csv_file = csv.writer(file)
    for dev in data["devs"]:
        for tag in dev["tags"]:
            csv_file.writerow([tag['id'], tag['name'], tag['dataType'], tag['description'], tag['alarm'], tag['value'], tag['quality'], tag['DevTagId']])



麻烦代码错误



Trouble Code with the error

import json
import csv

with open('C:\\folder\\dev\\TagAlarms.txt',"r") as file:
    data = json.load(file)

with open('C:\\folder\\dev\\TagAlarms.csv',"w",newline='') as file:
    csv_file = csv.writer(file)
    for dev in data["devs"]:
        for tag in dev["tags"]:
            for alarm in tag["alarmst"]:
                csv_file.writerow(alarm['dateStatus'],[alarm['dateStart'], alarm['status'], alarm['type']])



错误



The Error

    csv_file.writerow(alarm['dateStatus'], [alarm['dateStart'], alarm['status'], alarm['type']])
TypeError: string indices must be integers



样本数据



Sample Data

{
  "success": true,
  "moreDataAvailable": true,
  "devs": [
    {
      "id": 111111,
      "name": "dev123",
      "tags": [         
        {
          "id": 10100,
          "name": "CleanTask",
          "dataType": "Bool",
          "description": "",
          "alarmHint": "",
          "value": 0,
          "quality": "good",
          "alarmst": {
            "dateStatus": "2016-11-08T06:58:06Z",
            "dateStart": "2016-11-08T06:22:16Z",
            "status": "RTN",
            "type": "None"
          },


推荐答案

您的问题是标签[alarmst]中的报警:
csv_file.writerow(alarm ['dateStatus']的行:

Your issue is the line:

for alarm in tag["alarmst"]:
    csv_file.writerow(alarm['dateStatus'],alarm['dateStart'], ...)

请注意,在您的数据中, alarmst 是一个JSON对象,它在python中被翻译成一个字典。所以当你重复一遍的时候,你最终会得到这些键:ie alarm 将是dateStatus,dateStart,status。 ..

Notice that in your data, the value for alarmst is a JSON object, which in python is translated into a dictionary. So when you iterate over it, you end up with the keys: i.e. alarm will be "dateStatus", "dateStart", "status", ....

替换为:

alarm = tag["alarmst"]
csv_file.writerow(...)

这篇关于Python TypeError用于逻辑迭代数据值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆