码迷,mamicode.com
首页 > Web开发 > 详细

其他(handel_jsonl-format_date)

时间:2021-06-02 18:34:04      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:else   replace   data   use   odi   date   pen   日期   ict   

其他

1 处理jsonl文件

#!\Users\Local\Programs\Python37
# -*- coding: utf-8 -*-
import json
def save_data_jsonl(data_dict_list,path="./new_id_source.jsonl"):
    """将数据保存为jsonl格式"""
    with open(path,mode=a,encoding="utf-8") as fp:
        for data_dict in data_dict_list:
            fp.write(json.dumps(data_dict,ensure_ascii=False)+"\n")

def read_jsonl(path="./baidu.jsonl"):
    """读取jsonl文件,返回字典对象列表"""
    data_dict_list=[]
    with open(path,mode="r",encoding="utf-8") as fp:
        lines =fp.readlines()
        for line in lines:
            data_dict =json.loads(line)
            data_dict_list.append(data_dict)
    return data_dict_list

 

2 格式化时间日期

#!\Users\Local\Programs\Python37
# -*- coding: utf-8 -*-

import datetime
import re
def format_date(date):

    if "" in date:
        s = re.search(r"\d+", date)
        day =s.group()
        year =datetime.datetime.now().year
        month =datetime.datetime.now().month
    elif "昨天" in date:
        date_format = (datetime.datetime.now() + datetime.timedelta(days=-1)).strftime("%Y-%m-%d")
        print(date_format)
        return date_format
    else:
        date_format = date.replace("", "-").replace("", "-").replace("", "")
        date_split_list =date_format.split("-")
        # year =date_split_list[0] if len(date_split_list)==3 else datetime.datetime.now().year
        if len(date_split_list) ==3:
            year =date_split_list[0]
            month =date_split_list[1]
            day =date_split_list[2]
        else:
            year =datetime.datetime.now().year
            month = date_split_list[0]
            day = date_split_list[1]

    if int(month) < 10:
        month = "0" + str(month)
    if int(day) < 10:
        day = "0" + str(day)
    date_format = str(year) + "-" + str(month) + "-" + str(day)
    print("date_format",date_format)
    return date_format

format_date(7月21日)
format_date(2021年7月21日)
format_date("8天前")
format_date("昨天17:50")

 

其他(handel_jsonl-format_date)

标签:else   replace   data   use   odi   date   pen   日期   ict   

原文地址:https://www.cnblogs.com/huahuawang/p/14830639.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!