You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
50 lines
1.5 KiB
50 lines
1.5 KiB
import os
|
|
import pandas as pd
|
|
|
|
# 文件路径
|
|
input_file = r'D:\计量经济学\计量实验资料及作业要求\计量实验资料及作业要求\UGC回归数据.xlsx'
|
|
output_file = r'D:\计量经济学\计量实验资料及作业要求\计量实验资料及作业要求\UGC回归数据.xlsx'
|
|
|
|
print("========================================")
|
|
print(" 数据导入操作")
|
|
print("========================================")
|
|
print(f"输入文件: {input_file}")
|
|
print(f"输出文件: {output_file}")
|
|
print()
|
|
|
|
# 检查文件是否存在
|
|
if not os.path.exists(input_file):
|
|
print("错误: 输入文件不存在!")
|
|
exit(1)
|
|
|
|
print(f"文件大小: {os.path.getsize(input_file) / 1024:.2f} KB")
|
|
|
|
# 读取数据
|
|
try:
|
|
print("正在读取数据...")
|
|
df = pd.read_excel(input_file)
|
|
print(f"成功读取 {len(df)} 行数据")
|
|
print(f"列名: {list(df.columns)}")
|
|
print(f"数据类型:")
|
|
print(df.dtypes)
|
|
|
|
print("\n前5行数据:")
|
|
print(df.head())
|
|
|
|
# 写入到同一个文件
|
|
print("\n写入数据到目标文件...")
|
|
df.to_excel(output_file, index=False)
|
|
|
|
print(f"数据已成功导入到: {output_file}")
|
|
print(f"总行数: {len(df)}")
|
|
print(f"总列数: {len(df.columns)}")
|
|
|
|
print()
|
|
print("========================================")
|
|
print(" 数据导入完成")
|
|
print("========================================")
|
|
|
|
except Exception as e:
|
|
print(f"处理文件时出错: {str(e)}")
|
|
import traceback
|
|
traceback.print_exc()
|
|
|