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)
相关推荐
梦幻精灵_cq2 分钟前
sumdir——一个伪python.built-in的“术法”打造
python
weixin_440730501 小时前
函数return、参数、参数组、递归函数、高阶函数等小结
开发语言·python
天赐范式2 小时前
天赐范式第181天:让视图开始切换——PBFT视图切换与从safety到liveness的跨越
python·pbft·数字生命·天赐范式·动态运行时·拜占庭容错
sugarzhangnotes2 小时前
【无标题】
开发语言·人工智能·python
weixin_447195292 小时前
【无标题】
pytorch·python
迅猛龙办公室2 小时前
Python实现简单的人名对话
python
阿坨3 小时前
firestart:一行命令启动你的日常应用和网页
python·pypi·cli·click
老歌老听老掉牙3 小时前
麻花钻切屑形态演变的力学机制与临界条件分析
python·算法·钻头
happylifetree4 小时前
Python18(补充):练习
python
weixin_416667964 小时前
银河麒麟V10看门狗试验-1
网络·chrome·python