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)
相关推荐
不知名的老吴1 小时前
Lambda表达式与新的Streams API相结合
开发语言·python
weelinking7 小时前
【产品】12_接入数据库——让数据永久保存
jvm·数据库·python·react.js·数据挖掘·前端框架·产品经理
程序大视界8 小时前
【Python系列课程】Python正则表达式(下):环视、命名分组与日志实战
开发语言·python·正则表达式
TickDB8 小时前
美股行情 API 接入避坑:REST 快照、WebSocket 推送、盘前盘后数据的边界
人工智能·python·websocket·行情数据 api
枫叶v.9 小时前
Agent 分层存储架构设计:从记忆方法到中间件选型
开发语言·python
水兵没月9 小时前
逆向实战小记——某ToB商城网站分析学习
python·网络爬虫
程序员小远9 小时前
Python自动化测试框架及工具详解
自动化测试·软件测试·python·测试工具·职场和发展·测试用例·接口测试
sleven fung10 小时前
MinerU与BabelDOC与KTransformers与OpenAI API库
开发语言·python·ai·langchain
小毛驴85010 小时前
spring-boot-maven-plugin,maven-compiler-plugin 功能对比
java·python·maven
萤萤七悬10 小时前
【Python笔记】AI帮实现CLI工具-使用argparse.ArgumentParser接收命令参数
开发语言·笔记·python