码迷,mamicode.com
首页 > 编程语言 > 详细

python使用pandas读取excel

时间:2020-06-28 20:42:59      阅读:55      评论:0      收藏:0      [点我收藏+]

标签:utf-8   encoding   http   for   write   token   list()   dict   return   

# -*- coding: utf-8 -*-
import pandas as pd
from collections import defaultdict
import json
from pathlib import Path

DATA = "6.28"
dd = defaultdict(list)


def save_file(l, filename="sss"):
    _temp = Path().cwd()
    file_path = _temp / f"{filename}_list.txt"
    with file_path.open("w", encoding="utf-8") as fs:
        fs.write("\n".join(l))


def excel_to_list():
    sExcelFile = "./update_video_task.xlsx"
    df = pd.read_excel(sExcelFile, sheet_name=‘sheet1‘)
    df = df.fillna(method=‘ffill‘)
    df = df.loc[:, ~df.columns.str.contains(‘^Unnamed‘)]
    d = df.to_dict("records")
    return d


def gen_new_list(l, data=DATA):
    for d in l:
        if str(d["data"]) == data:
            name = d["name"]
            source_token = int(d["token_id"])
            mid = d["mid"]
            yield {"name": name, "source_token": int(source_token), "mid": mid}


def create_task_list():
    d = excel_to_list()
    weibo_list = []
    bilibili_list = []
    for item in gen_new_list(d):
        name = item["name"]
        mid = item["mid"]
        source_token = item["source_token"]
        dd[name].append((source_token, mid))
    for k, v in dd.items():
        mid_list = [_id[1] for _id in v]
        source_token = v[0][0]
        item = {"source_token": source_token, "mid_list": mid_list}
        url = f‘http://{{"data":[{json.dumps(item)}]}}‘

        task = f"{k} {url}"
        if k.startswith("zzz"):
            zzz_list.append(task)
        elif k.startswith("sss"):
           sss_list.append(task)

    save_file(ssss_list, filename="sss")
    save_file(zzz_list, filename="zzz")


if __name__ == ‘__main__‘:
    create_task_list()

python使用pandas读取excel

标签:utf-8   encoding   http   for   write   token   list()   dict   return   

原文地址:https://www.cnblogs.com/c-x-a/p/13204744.html

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