def save_to_sql(): df = ps.read_csv("./datas.csv",index_col=0) df.to_sql('movies_cop',con=engine,index=False,if_exists ='append')
时间: 2024-06-01 15:10:17 浏览: 132
This function reads a CSV file named "datas.csv" and saves its contents to a SQL database table named "movies_cop". The function uses the pandas library to read the CSV file and convert it into a pandas DataFrame. It then uses the to_sql method to save the DataFrame to the SQL database using the provided SQLalchemy engine. The if_exists parameter is set to 'append', which means that if the table already exists, the data will be added to the existing data.
相关问题
def huifeikouchu_login(request): if request.method in ["POST", "GET"]: msg = {'code': normal_code, "msg": mes.normal_code} req_dict = request.session.get("req_dict") datas = huifeikouchu.getbyparams(huifeikouchu, huifeikouchu, req_dict) if not datas: msg['code'] = password_error_code msg['msg'] = mes.password_error_code return JsonResponse(msg) try: __sfsh__= huifeikouchu.__sfsh__ except: __sfsh__=None if __sfsh__=='是': if datas[0].get('sfsh')!='是': msg['code']=other_code msg['msg'] = "账号已锁定,请联系管理员审核!" return JsonResponse(msg) req_dict['id'] = datas[0].get('id') return Auth.authenticate(Auth, huifeikouchu, req_dict)
这是一个Django的视图函数,用于处理前端发送的请求。如果请求的方法是POST或GET,会从会话中获取一个名为'req_dict'的字典,然后调用huifeikouchu.getbyparams()函数来获取指定条件下的数据。如果获取不到数据,则会返回一个表示密码错误的错误码和错误信息。如果获取到了数据,则会判断该用户是否已经被锁定,如果被锁定,则会返回一个表示账号被锁定的错误码和错误信息。如果账号没有被锁定,则会在req_dict中添加'id'字段,并调用Auth.authenticate()函数进行认证。
to_csv_name = 'basic.csv' to_csv_path = os.path.join(os.path.abspath( os.path.join(os.path.join(os.path.dirname(__file__), ".."), 'static/spider/data')), to_csv_name) pd_datas = pd.read_csv(to_csv_path, encoding='utf-8').to_dict(orient='record') to_data = []
这段代码是将一个名为"basic.csv"的文件读入为一个Pandas DataFrame对象,并将其转换为Python字典对象。该文件位于项目根目录下的"static/spider/data"文件夹中。然后,将字典数据存储在名为"to_data"的列表对象中。具体来说,该代码使用了Pandas库的read_csv函数读取CSV文件,并使用to_dict方法将DataFrame对象转换为字典对象。其中,orient参数指定了字典的形式,'record'表示每行数据将转换为一个字典对象。
阅读全文