老板要求你几十万条数据 全都放到xls文件里, 怎么办
启动程序, 替换好你的txt名称, 和最后输出的,xls文件
以及转换成功了,并且按照 格式 都分好组了
import pandas as pd
import os
def txt_to_excel_simple(txt_file_path, excel_file_path):
"""
将txt文件中的每一行原样导入到Excel的每一行中
"""
try:
# 检查txt文件是否存在
if not os.path.exists(txt_file_path):
print(f"错误:文件 {txt_file_path} 不存在")
return False
# 读取txt文件
with open(txt_file_path, 'r', encoding='utf-8') as file:
lines = file.readlines()
# 去除每行的换行符
cleaned_lines = [line.strip() for line in lines if line.strip()]
# 创建DataFrame,每行作为一个单元格
df = pd.DataFrame(cleaned_lines, columns=['Data'])
# 保存为Excel文件
df.to_excel(excel_file_path, index=False)
print(f"成功将数据导入到 {excel_file_path}")
print(f"共处理 {len(cleaned_lines)} 行数据")
return True
except Exception as e:
print(f"处理过程中出现错误: {e}")
return False
# 使用示例
if __name__ == "__main__":
# 输入文件路径
txt_file = "100000.txt_part_10.txt"
# 输出文件路径
excel_file = "100000.txt_part_10.xlsx"
# 执行转换
txt_to_excel_simple(txt_file, excel_file)
0