import pandas as pd
import openpyxl

file_name = '02-林业.xlsx'
wb = openpyxl.load_workbook(file_name, read_only=True)
name_lst = wb.sheetnames
stock_name = name_lst[0]

df_stock_values = pd.read_excel(file_name, sheet_name=stock_name)
lst_date = list(df_stock_values['trade_date'])
lst_open = list(df_stock_values['open'])
lst_close = list(df_stock_values['close'])
lst_high = list(df_stock_values['high'])
lst_low = list(df_stock_values['low'])

# 请编写函数,功能为,分别输出上市公司的名字列表和股票代码列表
# name_sh为上市公司名称和代码的列表,其形式为: ['平潭发展000592.SZ', 'ST景谷600265.SH']
# 请编写函数,功能为,输出输入列表的平均数
# 调用函数,分别输出当前的上市公司列表,和股票代码列表
#输出用户想要查询的公司名称及股票代码
def get_stock_names():
    name_sh = []
    for name in name_lst:
        if 'ST' in name:
            name_sh.append(name + '.' + 'SH')
        else:
            name_sh.append(name + '.' + 'SZ')
    return name_sh

def get_average(lst):
    return sum(lst) / len(lst)

company_lst = get_stock_names()
code_lst = [code.split('.')[0] for code in company_lst]

print('当前上市公司列表为:', company_lst)
print('当前股票代码列表为:', code_lst)

company_name = input('请输入要查询的公司名称:')
if company_name in company_lst:
    index = company_lst.index(company_name)
    print('您查询的公司名称为:', company_name)
    print('该公司的股票代码为:', code_lst[index])
else:
    print('您输入的公司名称不存在,请重新输入。')

#输出用户想要查询的公司名称及股票代码
Python Pandas 读取 Excel 文件并提取股票数据 - 代码示例

原文地址: http://www.cveoy.top/t/topic/nj5i 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录