python實現excel轉txt代碼

python實現excel轉txt代碼

excel_to_txt.py

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. pythonexceltxt

    2024-03-21 07:32:04       22 阅读
  2. 关于txt导入Excel

    2024-03-21 07:32:04       14 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-21 07:32:04       19 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-21 07:32:04       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-21 07:32:04       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-21 07:32:04       20 阅读

热门阅读

  1. SpringMVC

    SpringMVC

    2024-03-21 07:32:04      17 阅读
  2. 无人机拦截

    2024-03-21 07:32:04       20 阅读
  3. kafka 集群

    2024-03-21 07:32:04       19 阅读
  4. Kafka

    2024-03-21 07:32:04       20 阅读
  5. 【 React 】 在React项目中是如何使用Redux的?

    2024-03-21 07:32:04       21 阅读
  6. React——props children (插槽平替)

    2024-03-21 07:32:04       19 阅读
  7. 全量知识系统 概要设计(SmartChat回复)

    2024-03-21 07:32:04       21 阅读
  8. webRtc麦克风摄像头检测

    2024-03-21 07:32:04       18 阅读