我目前正在尝试使用Python词典来识别文件类型并输出将电子表格转换为熊猫数据框所需的功能。
它确实有效,但是除了我尝试上传扩展名为 .csv 的文件外,我得到的是: XLRDError
我只保存了正在运行 .csv
的 .xlsx 文件的副本。有更好的做法吗? (不使用“ if”)
Stuff.py
def execute(file_path):
read_map = {"xls": pd.read_excel, "xlsm": pd.read_excel, "xlsx": pd.read_excel,
"csv": pd.read_csv}
get_extension = os.path.splitext(file_path)[1].lower()[1:]
assert get_extension in read_map
assert os.path.isfile(file_path)
#reads convert into DataFrame
read_function = read_map[get_extension]
df = read_function(file_path)
return df
Fetch.py
import Stuff as s
UPLOAD_DIR = "/"
@app.route("/", methods=["GET", "POST"])
def index():
if request.method == "POST":
#get data
data = request.files["data_file"]
filename = secure_filename(data.filename)
abs_path = os.path.join(UPLOAD_DIR, filename)
#save the data
data.save(abs_path)
output_data = s.execute(abs_path)