Python常用代码: 数据读写(json, txt, html)

复制代码, 保存为.py文件, 在需要使用的模块调用即可(注意文件位置修改), 日志器内容参考上一篇文章, 需要引入的第三方库请自行下载…

import os
import json
from bs4 import BeautifulSoup
from utils.get_log import GetLog


# 获取日志器
log = GetLog.get_log()


def read_json(file_name):
    """
    读取json文件为python字典对象
    :param file_name: 文件名称(不含后缀)
    :return: 字典对象
    """
    try:
        with open("\\data\\{}.json".format(file_name), "r", encoding="utf-8") as f:
            result = json.load(f)
            return result
    except Exception as ex:
        log.error(ex)


def write_json(data_source, file_name):
    """
    写入python字典对象为json文件
    :param data_source: 字典对象
    :param file_name: 保存的文件名(不含后缀)
    :return: json文件
    """
    try:
        with open("\\data\\{}.json".format(file_name), "w", encoding="utf-8") as f:
            # # 先序列化, 解决默认ascii编码, 解决中文乱码问题
            # data = json.dumps(data_source, ensure_ascii=False, indent=4)
            json.dump(data_source, f, ensure_ascii=False)
    except Exception as ex:
        log.error(ex)


def read_txt(file_name):
    """
    读取txt文件为python列表对象, txt每行内容为列表的一个元素
    :param file_name: 要读取的文件名(不含后缀)
    :return: 列表对象
    """
    try:
        filepath = "\\data\\{}.txt".format(file_name)
        with open(filepath, "r", encoding="utf-8") as f:
            list_data = f.readlines()
            return [x.strip() for x in list_data]
    except Exception as ex:
        log.error(ex)


def write_txt(data_source, file_name):
    """
    保存数据(字符串)到txt文件(追加形式)
    :param data_source: 字符串
    :param file_name: 保存的文件名(不含后缀)
    :return: txt文件
    """
    try:
        filepath = "\\data\\{}.txt".format(file_name)
        with open(filepath, "a+", encoding="utf-8") as f:
            f.write(str(data_source) + '\n')
    except Exception as ex:
        log.error(ex)


def save_html(dir_name, file_name, file_content):
    """
    保存html文件
    :param dir_name: 设置保存的位置
    :param file_name: 设置保存的文件名(不含后缀)
    :param file_content: 保存的内容(例如接口返回的html文本-[res.text])
    :return: html文件
    """
    try:
        path = "\\data\\{}".format(dir_name)
        if not os.path.exists(path):
            os.makedirs(path)
        with open("{}\\{}.html".format(path, file_name), "w", encoding="utf-8") as f:
            f.write(file_content)
    except Exception as ex:
        log.error(ex)


def read_html(dir_name, file_name):
    """
    读取html文件
    :param dir_name: 文件路径名
    :param file_name: 文件名(不含后缀)
    :return: bs4对象(通常配合bs4使用)
    """
    try:
        with open("\\data\\{}\\{}.html".format(dir_name, file_name), "r", encoding="utf-8") as f:
            return BeautifulSoup(f, 'lxml')
    except Exception as ex:
        log.error(ex)
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Nonevx

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值