python - 更改字典列表中的相同值

标签 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 的值更改为我需要的格式。任何帮助或指出正确的方向将不胜感激。

最佳答案

'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'}]

关于python - 更改字典列表中的相同值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53322571/

相关文章:

python - matplotlib 中的误差线显示在其他曲线上

Python requests.get 持续超时大约一分钟,然后继续正常工作

python - Django 按年分页

python - 用 python 编写快速代码

python-3.x - 广度优先搜索 : Shortest Path using Python

c# - 在 SSIS .net 脚本任务中格式化 Excel 列

python-3.x - mprof run <executable> 让我得到 "Command not found"

python - Camelot python;OSError : exception: access violation writing 0x00000080

sql - 将列的值分配给sql中的变量使用jinja模板语言

error-handling - Nifi : send file to HDFS; if fail, wait one second then retry