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.
69 lines
2.2 KiB
69 lines
2.2 KiB
import os
|
|
import csv
|
|
|
|
# 文件路径
|
|
output_file = r'D:\计量经济学\计量实验资料及作业要求\计量实验资料及作业要求\UGC回归数据.csv'
|
|
|
|
print("========================================")
|
|
print(" 创建并填充UGC回归数据")
|
|
print("========================================")
|
|
print(f"输出文件: {output_file}")
|
|
print()
|
|
|
|
# 检查输出目录是否存在
|
|
output_dir = os.path.dirname(output_file)
|
|
print(f"输出目录: {output_dir}")
|
|
print(f"目录存在: {os.path.exists(output_dir)}")
|
|
|
|
if not os.path.exists(output_dir):
|
|
print("正在创建输出目录...")
|
|
try:
|
|
os.makedirs(output_dir)
|
|
print("目录创建成功")
|
|
except Exception as e:
|
|
print(f"创建目录失败: {e}")
|
|
exit(1)
|
|
|
|
# 创建并填充CSV文件
|
|
try:
|
|
print("\n创建并填充CSV文件...")
|
|
with open(output_file, 'w', newline='', encoding='utf-8-sig') as f:
|
|
writer = csv.writer(f)
|
|
|
|
# 写入表头
|
|
headers = ['Y', 'X1', 'X2', 'X3', 'X4', 'X5', 'X6']
|
|
writer.writerow(headers)
|
|
|
|
# 写入示例数据(前10行)
|
|
for i in range(1, 11):
|
|
row = [
|
|
i * 0.5, # Y: UGC有用性
|
|
i * 2, # X1: 评论数量
|
|
i * 10, # X2: 评论长度
|
|
i * 2, # X3: 评论复杂度
|
|
5.0, # X4: 评论可读性
|
|
(i % 3) - 1, # X5: 内容情感性
|
|
i * 0.3 # X6: 信息丰富度
|
|
]
|
|
writer.writerow(row)
|
|
|
|
print(f"文件已成功创建: {output_file}")
|
|
print(f"文件大小: {os.path.getsize(output_file) / 1024:.2f} KB")
|
|
|
|
# 读取并显示文件内容
|
|
print("\n文件内容:")
|
|
with open(output_file, 'r', encoding='utf-8-sig') as f:
|
|
reader = csv.reader(f)
|
|
for i, row in enumerate(reader):
|
|
if i < 5:
|
|
print(f"行 {i+1}: {row}")
|
|
|
|
print()
|
|
print("========================================")
|
|
print(" 任务完成")
|
|
print("========================================")
|
|
|
|
except Exception as e:
|
|
print(f"处理文件时出错: {str(e)}")
|
|
import traceback
|
|
traceback.print_exc()
|
|
|