Files
HKDataManagment/UpdateFutuData/updateExcel.py
2025-08-26 10:32:50 +08:00

31 lines
1.2 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import pandas as pd
# 检查文件是否存在且有效
import os
if not os.path.exists('202410_202508_平均流通市值.xlsx'):
print("文件不存在")
else:
# 尝试用 openpyxl 直接打开检查
from openpyxl import load_workbook
try:
wb = load_workbook('202410_202508_平均流通市值.xlsx')
print(f"文件中包含的工作表: {wb.sheetnames}")
except Exception as e:
print(f"文件损坏或不是有效的Excel文件: {e}")
# 读取原始Excel文件
df = pd.read_excel('202410_202508_平均流通市值.xlsx') # 假设你的文件名为"股票数据.xlsx"
# 筛选股票名称最后一个字符不是"R"的股票
# 方法1使用字符串的.endswith()方法
filtered_df = df[~df['股票名称'].str.endswith('R')]
# 方法2使用正则表达式
# filtered_df = df[~df['股票名称'].str.contains('R$')] # $表示字符串结尾
# 方法3使用字符串切片
# filtered_df = df[df['股票名称'].str[-1] != 'R']
# 将筛选结果写入新Excel文件
filtered_df.to_excel('202410_202508_平均流通市值_不带R的股票.xlsx', index=False)
print("筛选完成,结果已保存到'筛选结果_不带R的股票.xlsx'")