用烧瓶上传并显示数据框| AttributeError:“ builtin_function_or_method”对象没有属性“ replace”

问题描述

我正在尝试按烧瓶上传显示数据框,当我想要显示它时,它说

AttributeError: 'builtin_function_or_method' object has no attribute 'replace'. 

我在YT上找到了此代码,不知道它是否正确。有人可以帮我吗?

from flask import Flask,render_template,request
from werkzeug.utils import secure_filename
import pandas as pd
import csv

def reencode(file):
    for line in file:
       yield line.decode('windows-1250').encode('utf-8')

@app.route("/data")
def data():
  df = pd.read_csv("Sistema_de_Stock.csv",encoding='latin-1')
  df = df.drop(df.loc['stock al cargar':].columns,axis=1)
  df.to_html('data.html')
  with open("data.html",'r',encoding='latin-1') as file:
    file = file.read
  **file = file.replace("<table","<table class='rwd-table'")**
  with open("data.html","w") as file_write:
    file_write.write(html + file)
  data = os.startfile("data.html")

  return data

解决方法

file.read方法,因此您应该调用该方法。此外,您可能想重命名该变量,以明确表明这不是文件处理程序:

with open('data.html','r',encoding='latin-1') as file:
    #    call the method ↓
    file_data = file.read().replace('<table',"<table class='rwd-table'")
  with open('data.html','w') as file_write:
    file_write.write(html + file_data)
  data = os.startfile('data.html')