简体   繁体   English

NameError:名称“`Timevalue`”未定义

[英]NameError: name '`Timevalue`' is not defined

I store data in csv file as follwing: 我将数据存储在csv文件中,如下所示:

row = dict_items([('Time ': '2017-12-01T13:54:04'), ('Energy [kWh]': '0.01'), ('Voltage [V]': '221.64'), ('Current [A]': '0.08')])

Now i want to store it like that: 现在我想像这样存储它:

('Time ': '2017-12-01T13:54:04', 'Energy [kWh]': '0.01'),
('Time ': '2017-12-01T13:54:04','Voltage [V]': '221.64'), 
('Time ': '2017-12-01T13:54:04','Current [A]': '0.08')])

so i wrote this code bellow and i define 所以我写了下面的代码,我定义

    Device=""
    Value=""
    for key, value in row.items():
        print(row.items())
        if key == 'Time':
            Timevalue = value
            print(Zeitvalue)
        Device = key
        Value = value
        doc = {'Device':Device, 'Measure':Value , 'Time':Timevalue }

i got this error: NameError: name ' Timevalue ' is not defined How can i make the Timevalue variable globale to avoid this Problem? 我收到此错误:NameError:名称' Timevalue '未定义我如何使Timevalue变量Timevalue以避免此问题? Thank you 谢谢

That ('Time ': '2017-12-01T13:54:04', 'Energy [kWh]': '0.01') syntax is not valid Python. 那个('Time ': '2017-12-01T13:54:04', 'Energy [kWh]': '0.01')语法无效的Python。 Assuming you actually want a list of dictionaries, it's not too hard to perform that conversion. 假设您实际上想要一个字典列表,那么执行该转换并不难。

You first need to grab the time stamp so it can be combined with each of the other items, and then you can drop it from the dict to make it easier to copy the remaining items to the new structure. 您首先需要获取时间戳,以便可以将其与其他每个项目组合在一起,然后可以将其从dict中删除,以便更轻松地将其余项目复制到新结构中。 Note that this modifies the original dict passed to the function. 请注意,这会修改传递给函数的原始字典。 If you don't want that, you can either make a copy of the passed-in dict, or put an if test in the loop that copies the data to the new structure so that the time item is skipped. 如果您不希望这样做,则可以复制传入的字典,也可以将if测试放入将数据复制到新结构的循环中,从而跳过时间项。

def convert(old):
    time_key = 'Time '
    # Save the time
    time_item = (time_key, old[time_key])
    # Add remove it
    del old[time_key]
    # Copy remaining items to new dicts and save them in a list
    return [dict([time_item, item]) for item in old.items()]

row = {
    'Time ': '2017-12-01T13:54:04', 
    'Energy [kWh]': '0.01', 
    'Voltage [V]': '221.64', 
    'Current [A]': '0.08',
}

new_data = convert(row)
for d in new_data:
    print(d)

output 输出

{'Time ': '2017-12-01T13:54:04', 'Energy [kWh]': '0.01'}
{'Time ': '2017-12-01T13:54:04', 'Voltage [V]': '221.64'}
{'Time ': '2017-12-01T13:54:04', 'Current [A]': '0.08'}

Here's how to do it if you don't want to mutate or copy the original dict: 如果您不想变异或复制原始字典,请按以下步骤操作:

def convert(old):
    time_key = 'Time '
    # Save the time
    time_item = (time_key, old[time_key])
    # Copy other items to new dicts and save them in a list
    return [dict([time_item, (key, val)]) 
        for key, val in old.items() if key != time_key]

Note that this is less efficient because it has to test every key to ensure it's not the time key. 请注意,这效率较低,因为它必须测试每个键以确保它不是时间键。


To save the data in a list of OrderedDict s, we need to change the logic slightly. 要将数据保存在OrderedDict的列表中,我们需要稍微更改逻辑。 We also need to create the OrderedDict s properly so that the items are in the desired order. 我们还需要正确创建OrderedDict ,以使项目按所需顺序排列。

from collections import OrderedDict
from pprint import pprint

def convert(row):
    time_key = 'Time '
    time_value = row[time_key]
    new_data = []
    for key, val in row.items():
        if key == time_key:
            continue
        new_data.append(OrderedDict(Device=key, Measure=val, Time=time_value))
    return new_data

row = {
    'Time ': '2017-12-01T13:54:04', 'Energy [kWh]': '0.01',
    'Voltage [V]': '221.64', 'Current [A]': '0.08'
}

new_data = convert(row)
pprint(new_data)

output 输出

[OrderedDict([('Device', 'Energy [kWh]'),
              ('Measure', '0.01'),
              ('Time', '2017-12-01T13:54:04')]),
 OrderedDict([('Device', 'Voltage [V]'),
              ('Measure', '221.64'),
              ('Time', '2017-12-01T13:54:04')]),
 OrderedDict([('Device', 'Current [A]'),
              ('Measure', '0.08'),
              ('Time', '2017-12-01T13:54:04')])]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM