import os
import sys
import pymysql, traceback
import pandas as pd
from typing import List
# database_flag = sys.argv[1]
# DATABASE_CONFIG = {
# "test": {#'user': 'xxxxxxx'#,'passwd': 'xxxxxx'#,'host': 'xx.xx.xx.xx'#,'database': 'xxxx'#,'port': 3306# #}"prod": {#'user': 'xxxxx'#,'passwd': 'xxxxxx'#,'host': 'xx.xx.xx.xx'#,'database': 'xxxxxx'#,'port': 3306
# }
# }
# db = pymysql.connect(host=DATABASE_CONFIG.get(database_flag).get("host"),
# user=DATABASE_CONFIG.get(database_flag).get("user"),
# password=DATABASE_CONFIG.get(database_flag).get("passwd"),
# database=DATABASE_CONFIG.get(database_flag).get("database"),
# port=DATABASE_CONFIG.get(database_flag).get("port"),
# charset="utf8")
#
# cursor = db.cursor(cursor=pymysql.cursors.DictCursor)
def get_data_from_excel(excel_path, sheet_name, header, usecols, names):
"""Read the Excel table"""
try:
data = pd.read_excel(excel_path, sheet_name=sheet_name, header=header,
usecols=usecols,
names=names
)
print(f"data = {data}")
for index, row in data.iterrows():
name = row['name']
age = row['age']
print(f"name = {name} | age = {age}")
except:
traceback.print_exc()
if __name__ == "__main__":
tryExcel excel_path = os.getcwd() +'/test.xlsx'Print (f"os.getcwd() = {os.getcwd()}")
print(f"excel_path = {excel_path}"# header: Specifies the table header for the data table. The default value is0# index_COL: Used as the column number or name of the row index, or multiple row indexes if given a sequence. Index_col = False specifies that pandas does not use the first column as a row index. # usECOLS: Reads the specified column, also by name or index value sheet_name =0# sheet position, subscript from0Start the header =0# locate the header usECols ="A,B"
names = ['name'.'age']
data_list = get_data_from_excel(excel_path, sheet_name, usecols, names)
except BaseException:
traceback.print_exc()
Copy the code