python實現excel轉txt代碼

python實現excel轉txt代碼

excel_to_txt.py

bash 复制代码
from datetime import datetime, timedelta
import os
import pytz
import pandas as pd

def excel_to_txt(name, date):
    # Read Excel file into a DataFrame
    # df = pd.read_excel(f'/opt/module/data/excel/{name}.xlsx', header=None,skiprows=1)
    # df = pd.read_excel(f'hdfs://mycluster:8020/origin_data/hr_cn/db/is_gs_recruitment_data_full/excel/{name}.xlsx', header=None,skiprows=1)
    df = pd.read_excel(f'C:/Users/SARAH.X/PycharmProjects/pythonProject/data/excel/{name}.xlsx', header=None, skiprows=1)

    # Define output directory and path
    # output_directory = os.path.join('/opt/module/data', 'txt', date)
    output_directory = os.path.join('C:/Users/SARAH.X/PycharmProjects/pythonProject/data/', 'txt', date)
    os.makedirs(output_directory, exist_ok=True)  # Create directory if it doesn't exist
    output_path = os.path.join(output_directory, f'{name}.txt')

    # Check if the file already exists, if so, remove it
    if os.path.exists(output_path):
        os.remove(output_path)
        print(f'Existing file {output_path} removed.')

    # Write DataFrame to a new text file
    print('开始写入txt文件')
    df.to_csv(output_path, header=None, sep='\t', index=False)
    print('文件写入成功!')
    return output_path

if __name__ == '__main__':
    current_time = datetime.now(pytz.timezone('Asia/Shanghai'))
    one_day_ago = (current_time - timedelta(days=1)).strftime('%Y-%m-%d')
    local_file_path = excel_to_txt('example4', one_day_ago)
    print(local_file_path)
相关推荐
kyle~6 小时前
FastAPI --- 将业务能力以 API 的形式提供给外部系统
python·fastapi·前后端
aiotto资源分享6 小时前
Python 写 JSON 文件,程序中途退出会留下什么?
java·python·json·jsondecodeerror
梯度下降者7 小时前
Python信号量,条件变量,事件学习
python·条件变量·事件·信号量·线程通信
MSTcheng.8 小时前
【AI应用实战】用 FastAPI + SSE 做一个自己的 AI 聊天网页:流式输出、多轮对话与完整源码
人工智能·python·大模型·fastapi·sse·ai聊天助手
the局外人8 小时前
Python 实战检索优化:BM25、向量与重排
数据库·后端·python
__underline__8 小时前
冷门Python知识点006篇:Python 类体里的名字,能被"凭空造出来"
后端·python
用户0332126663679 小时前
使用 Python 轻松移除 Excel 文档中的重复行
python·excel
LiaoWL1239 小时前
从零搭一个 RAG 知识库(二):换上语义 Embedding——BGE 实测与阈值重校准
python·大模型·numpy·embedding·向量检索·rag
AI英德西牛仔9 小时前
智谱清言导出word指令失效?PC端深度用户正在用“AI导出鸭”重构内容归档流
人工智能·重构·word·excel·deepseek·ai导出鸭
Dmz丶9 小时前
前端转 AI 100 天 Day 48:向量数据库——100 万向量的毫秒级检索
python