目标:
修改二维列表的指定列信息,有匹配关系。而且,将日期转换成年月日的形式。
from datetime import datetime
# 原始数据列表
data = [
["下单时间", "建单时间","匹配列", "支付时间"],
['1713093431000',1715331791000,"zaide1",'1713093431000'],
['1713093431000',1715331791000,"zaide3",'1713093431000'],
['1713093431000',1715331791000,"zaide2",'1713093431000']
]
titleList1=["下单时间","建单时间","支付时间"]
dict1={"zaide1":"1","zaide3":"3","zaide2":"2"}
# 时间戳转换函数
def timestamp_to_date(timestamp_str):
# 将时间戳从字符串转换为整数,并从毫秒转换为秒
timestamp_int = int(timestamp_str) / 1000
# 转换为datetime对象
date_obj = datetime.fromtimestamp(timestamp_int).strftime('%Y-%m-%d')
return date_obj
# 遍历数据列表,转换时间戳列
for row in data[1:]: # 从第二行开始,因为第一行是标题行
for title in titleList1:
position=data[0].index(title)
row[position] = timestamp_to_date(row[position])
i =data[0].index("匹配列")
row[i] = dict1.get(row[i],row[i])
# 打印修改后的列表
print(data)
[['下单时间', '建单时间', '匹配列', '支付时间'], ['2024-04-14', '2024-05-10', '1', '2024-04-14'], ['2024-04-14', '2024-05-10', '3', '2024-04-14'], ['2024-04-14', '2024-05-10', '2', '2024-04-14']]