在词典列表中更改相同的值

时间:2018-11-15 15:19:25

标签: python python-3.x etl

我正在尝试提取与特定代码有关的特定数据。我设法将数据提取到新的词典列表中,但是我需要将日期格式从“ Y-m-d H:M:S”更改为“ d / m / Y”。

data =  [
    {'collection_date_time': '2018-05-01 00:00:00', 'code': '96423', 'result': '0.32'},
    {'collection_date_time': '2018-05-01 00:00:00', 'code': '96423', 'result': '0.16'},
    {'collection_date_time': '2018-05-01 00:00:00', 'code': '96423', 'result': '0.18'},
    {'collection_date_time': '2017-12-24 13:04:00', 'code': '45674', 'result': '0.43'},
    {'collection_date_time': '2017-12-24 13:04:00', 'code': '45674', 'result': '0.33'},
    {'collection_date_time': '2017-12-24 13:04:00', 'code': '45674', 'result': '0.01'},
    {'collection_date_time': '2018-04-08 09:43:00', 'code': '12343', 'result': '0.53'},
    {'collection_date_time': '2018-04-08 09:43:00', 'code': '12343', 'result': '0.22'},
    {'collection_date_time': '2018-04-08 09:43:00', 'code': '12343', 'result': '0.12'},
]

specific_code_data= []
def find_result_with_specific_code(x):
  global specific_code_data
  for d in test_data.ilab_data:
    if d['code'] == x:
        specific_code_data.append(d)
return specific_code_data

我尝试按照以下方式写东西:

for d in specific_code_data:
    indate = d['collection_date_time']
    dt_obj = datetime.datetime.strptime(indate, '%Y-%m-%d %H:%M:%S')
    dt_str = datetime.datetime.strftime(dt_obj, '%d/%m/%Y')

但是我不确定如何将字典列表中的键collection_date_time的值更改为所需的格式。任何帮助或指出正确的方向将不胜感激。

3 个答案:

答案 0 :(得分:0)

我认为您在这里错过了一步。您尚未重新分配修改后的字符串:

for d in specific_code_data:
    indate = d['collection_date_time']
    dt_obj = datetime.datetime.strptime(indate, '%Y-%m-%d %H:%M:%S')
    dt_str = datetime.datetime.strftime(dt_obj, '%d/%m/%Y')
    d['collection_date_time'] = dt_str` # you need to do this

您也可以尝试使用其他功能进行日期转换:

specific_code_data= []
def find_result_with_specific_code(x):
  global specific_code_data
  for d in test_data.ilab_data:
    if d['code'] == x:
        z = change_datetime_str(d) # line I added
        specific_code_data.append(z) # changed d to z
return specific_code_data

答案 1 :(得分:0)

怎么样呢?

from datetime import datetime

data =  [
    {'collection_date_time': '2018-05-01 00:00:00', 'code': '96423', 'result': '0.32'},
    {'collection_date_time': '2018-05-01 00:00:00', 'code': '96423', 'result': '0.16'},
    {'collection_date_time': '2018-05-01 00:00:00', 'code': '96423', 'result': '0.18'},
    {'collection_date_time': '2017-12-24 13:04:00', 'code': '45674', 'result': '0.43'},
    {'collection_date_time': '2017-12-24 13:04:00', 'code': '45674', 'result': '0.33'},
    {'collection_date_time': '2017-12-24 13:04:00', 'code': '45674', 'result': '0.01'},
    {'collection_date_time': '2018-04-08 09:43:00', 'code': '12343', 'result': '0.53'},
    {'collection_date_time': '2018-04-08 09:43:00', 'code': '12343', 'result': '0.22'},
    {'collection_date_time': '2018-04-08 09:43:00', 'code': '12343', 'result': '0.12'},
]

def alter_datetime_format(entry):
    collection_date_time = datetime.strptime(entry["collection_date_time"], "%Y-%m-%d %H:%M:%S")
    entry["collection_date_time"] = datetime.strftime(collection_date_time, "%d/%m/%Y")
    return entry

def find_result_with_specific_code(x, data):
    return [
        alter_datetime_format(entry)
        for entry in data
        if entry["code"] == x
    ]

答案 2 :(得分:0)

'but I need to changed the date format from 'Y-m-d H:M:S' to 'd/m/Y'

我也会跳到这里...使用pandas

data =  [
    {'collection_date_time': '2018-05-01 00:00:00', 'code': '96423', 'result': '0.32'},
    {'collection_date_time': '2018-05-01 00:00:00', 'code': '96423', 'result': '0.16'},
    {'collection_date_time': '2018-05-01 00:00:00', 'code': '96423', 'result': '0.18'},
    {'collection_date_time': '2017-12-24 13:04:00', 'code': '45674', 'result': '0.43'},
    {'collection_date_time': '2017-12-24 13:04:00', 'code': '45674', 'result': '0.33'},
    {'collection_date_time': '2017-12-24 13:04:00', 'code': '45674', 'result': '0.01'},
    {'collection_date_time': '2018-04-08 09:43:00', 'code': '12343', 'result': '0.53'},
    {'collection_date_time': '2018-04-08 09:43:00', 'code': '12343', 'result': '0.22'},
    {'collection_date_time': '2018-04-08 09:43:00', 'code': '12343', 'result': '0.12'},
]

sample = pd.DataFrame([d for d in data])

sample['collection_date_time'] = pd.to_datetime(sample['collection_date_time']).dt.normalize()
sample['collection_date_time'] = sample['collection_date_time'].dt.strftime("%d/%m/%Y")

data = []
for value in sample.values:
    list_of_dict = {}
    list_of_dict['code'] = value[0]
    list_of_dict['collection_date_time'] = value[1]
    list_of_dict['result'] = value[2]
    data.append(list_of_dict)

print(data)

[{'code': '96423', 'collection_date_time': '01/05/2018', 'result': '0.32'},
 {'code': '96423', 'collection_date_time': '01/05/2018', 'result': '0.16'},
 {'code': '96423', 'collection_date_time': '01/05/2018', 'result': '0.18'},
 {'code': '45674', 'collection_date_time': '24/12/2017', 'result': '0.43'},
 {'code': '45674', 'collection_date_time': '24/12/2017', 'result': '0.33'},
 {'code': '45674', 'collection_date_time': '24/12/2017', 'result': '0.01'},
 {'code': '12343', 'collection_date_time': '08/04/2018', 'result': '0.53'},
 {'code': '12343', 'collection_date_time': '08/04/2018', 'result': '0.22'},
 {'code': '12343', 'collection_date_time': '08/04/2018', 'result': '0.12'}]