程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了此代码创建表并将 csv 文件加载到其中。我试图将其转换为 OOP,但我不确定大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决此代码创建表并将 csv 文件加载到其中。我试图将其转换为 OOP,但我不确定?

开发过程中遇到此代码创建表并将 csv 文件加载到其中。我试图将其转换为 OOP,但我不确定的问题如何解决?下面主要结合日常开发的经验,给出你关于此代码创建表并将 csv 文件加载到其中。我试图将其转换为 OOP,但我不确定的解决方法建议,希望对你解决此代码创建表并将 csv 文件加载到其中。我试图将其转换为 OOP,但我不确定有所启发或帮助;

这是我的原始代码。

 import sqlite3,csv

import sqlalchemy

import sqlalchemy as db
from sqlalchemy import create_ENGIne

#create tables in databank
conn = sqlite3.connect ("matching.db")
cur = conn.cursor()

sql_Trainingtable = """
    create table IF NOT EXISTS Trainingtable (
        x REAL,y1 REAL,y2 REAL,y3 REAL,y4 REAL
    ) """
 
sql_IDealtable =    """
    create table IF NOT EXISTS IDealtable (
        x REAL,y4 REAL,y5 REAL,y6 REAL,y7 REAL,y8 REAL,y9 REAL,y10 REAL,y11 REAL,y12 REAL,y13 REAL,y14 REAL,y15 REAL,y16 REAL,y17 REAL,y18 REAL,y19 REAL,y20 REAL,y21 REAL,y22 REAL,y23 REAL,y24 REAL,y25 REAL,y26 REAL,y27 REAL,y28 REAL,y29 REAL,y30 REAL,y31 REAL,y32 REAL,y33 REAL,y34 REAL,y35 REAL,y36 REAL,y37 REAL,y38 REAL,y39 REAL,y40 REAL,y41 REAL,y42 REAL,y43 REAL,y44 REAL,y45 REAL,y46 REAL,y47 REAL,y48 REAL,y49 REAL,y50 REAL
    ) """

cur.execute(sql_TrainingtablE)
cur.execute(sql_IDealtablE)
conn.commit()
conn.close()



#insert values into Trainingtable
connection = sqlite3.connect("matching.db")
cursor = connection.cursor()

with open (r"C:\Users\Cedric#\Desktop\IUBH\Python\Train.csv","r") as file:
    no_records_Training = 0
    for row in file:
        cursor.execute("INSERT INTO Trainingtable VALUES (?,?,?)",row.split(","))
        connection.commit()
        no_records_Training += 1


#insert values into IDealtable
with open (r"C:\Users\Cedric#\Desktop\IUBH\Python\IDeal.csv","r") as file:
    no_records_IDeal = 0
    for row in file:
        cursor.execute("INSERT INTO IDealtable VALUES (?,"))
        connection.commit()
        no_records_IDeal += 1
connection.close()

这是我尝试构建的 OOP 代码。

import sqlite3,csv
import sqlalchemy
import sqlalchemy as db
from sqlalchemy import create_ENGIne

class DatabaseInterface:
    def __init__(self,file_path,query,database_Name):
        """
        Loads csv filE into sqlite database
        """
        self.file_path = file_path
        self.query = query
        self.conn = sqlite3.connect (database_Name)
        self.cur = conn.cursor()
    
    
    def create_table(self):
        sql_Trainingtable = """
            create table IF NOT EXISTS Trainingtable (
                x REAL,y4 REAL
            ) """
 
        sql_IDealtable =    """
            create table IF NOT EXISTS IDealtable (
                x REAL,y50 REAL
            ) """
    self.cur.execute(sql_TrainingtablE)
    self.cur.execute(sql_IDealtablE)
    
    def closeandcommit (self):
        self.databaseinterface.commit()
        self.databaseinterface.close()
    
    def add_values_from_csv_file(self):
        with open (r+{self.file_path},"r") as file:
            no_records_IDeal = 0
            for row in file:
                cursor.execute({self.query},"))
                connection.commit()
                no_records_IDeal += 1

我从来没有写过 OOP 代码,而且我对 Python 真的很陌生。任何帮助表示赞赏。我对方法和属性感到困惑。我是否必须在对象中的 self 方法之后添加更多内容?我从来没有写过 OOP 代码,而且我对 Python 真的很陌生。任何帮助表示赞赏。我对方法和属性感到困惑。我是否必须在对象中的 self 方法之后添加更多内容?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

大佬总结

以上是大佬教程为你收集整理的此代码创建表并将 csv 文件加载到其中。我试图将其转换为 OOP,但我不确定全部内容,希望文章能够帮你解决此代码创建表并将 csv 文件加载到其中。我试图将其转换为 OOP,但我不确定所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。