回到顶部

阅读目录

pandas read_csv、read_excel 填充合并的单元格

在使用 pandas 处理表格数据的时候,有时候表格里有很多合并的单元格,不想手动去取消合并再填充数据,应该怎么办呢?主要是使用:

        # 有合并的单元格,填充 NaN 数据
        data = data.fillna(method='pad')

代码:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
@author: yinzhuoqun
@site: http://zhuoqun.info/
@email: yin@zhuoqun.info
@time: 2019/4/22 15:22
"""
import os
import time
import requests
import pandas as pd


DESKTOP = os.path.join(os.path.expanduser("~"), "Desktop")  # 桌面


class FormToMany:
    def __init__(self, file_path, file_save_path=DESKTOP, api_url=None):
        self.file_path = file_path
        self.file_save_path = file_save_path
        self.api_url = api_url

    def to_json(self):
        """
        转变成 json 对象
        :return:
        """
        if self.file_path.endswith(".csv"):
            # converters 转换数据类型:citycode(列名称)
            data = pd.read_csv(self.file_path, encoding='utf-8', converters={'citycode': str})
            # data = pd.read_csv(self.file_path, encoding='gb2312', converters={'citycode': str})
        else:
            # converters 转换数据类型:citycode(列名称)
            data = pd.read_excel(self.file_path, encoding='utf-8', converters={'citycode': str})
            # data = pd.read_excel(self.file_path, encoding='gb2312', converters={'citycode': str})

        # 有合并的单元格,填充 NaN 数据
        data = data.fillna(method='pad')
        # force_ascii,是否使用 ASCII 码
        data = data.to_json(orient="index", force_ascii=False)
        return data

    def to_json_file(self):
        """
        保存到 json 文件
        :return:
        """
        current_date = time.strftime("%Y%m%d_%H%M%S")
        if self.file_path.endswith(".csv"):
            # 违规
            file_save_name = "order_illegal_%s.json" % current_date
        else:
            # 维权
            file_save_name = "order_rights_%s.json" % current_date
        try:
            with open(os.path.join(self.file_save_path, file_save_name), "w", encoding='utf-8') as f:
                f.write(self.to_json())
            print("提示:数据导出成功 %s" % os.path.join(self.file_save_path, file_save_name))
            return True
        except Exception as e:
            print(str(e))
            return False

    def to_json_post(self):
        """
        上传 json 对象
        :return:
        """
        if self.file_path.endswith(".csv"):
            kind = "csv"
        else:
            kind = "excel"
        body = {
            "type": kind,
            "data": self.to_json()
        }
        try:
            req = requests.post(self.api_url, data=body, timeout=180)
        except Exception as e:
            print(str(e))
            return False
        else:
            if req.status_code == 200:
                print("数据上传成功")
                return True
            else:
                print("数据上传结束")
                return False


if __name__ == "__main__":
    pass

效果图:

^_^
请喝咖啡 ×

文章部分资料可能来源于网络,如有侵权请告知删除。谢谢!

前一篇: 移动终端指数链接-艾瑞数据
下一篇: FiddlerDecryption(fiddler Inspectors 插件开发)
captcha