Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python CSV和SQLITE具有不同的列和值_Python_Pandas_Sqlite_Csv - Fatal编程技术网

Python CSV和SQLITE具有不同的列和值

Python CSV和SQLITE具有不同的列和值,python,pandas,sqlite,csv,Python,Pandas,Sqlite,Csv,我将csv文件导入Python并尝试使用SQLLite。但是当我创建表时,列和值都被更改了。例如,在“总体”列中显示值“asin”。reviewerID列收到值“总体”等等。 如何修复它 class csvrd(object): def csvFile(self): self.readFile('reviews.csv') def readFile(self, filename): conn = sqlite3.connect('amazonR

我将csv文件导入Python并尝试使用SQLLite。但是当我创建表时,列和值都被更改了。例如,在“总体”列中显示值“asin”。reviewerID列收到值“总体”等等。 如何修复它

class csvrd(object):
    def csvFile(self):

        self.readFile('reviews.csv')

    def readFile(self, filename):
        conn = sqlite3.connect('amazonReviews.db')
        cur = conn.cursor()             
        cur.execute("""CREATE TABLE IF NOT EXISTS amazonReviews(reviewerID varchar, asin INT,reviewerName varchar,reviewText varchar, overall INT,summary varchar,unixReviewTime INTEGER,reviewTime INTEGER,helpful INT,total INT)""")

        filename.encode('utf-8')
        print("Amazon Reviews table executed")
        with open(filename) as f:
            reader = csv.reader(f)
            for field in reader:
                cur.execute("INSERT INTO amazonReviews VALUES (?,?,?,?,?,?,?,?,?,?);", field)

        print("CSV Loaded into SQLite")
        conn.commit()
        conn.close()

c = csvrd().csvFile()


con = sqlite3.connect('amazonReviews.db')

pd.read_sql_query("SELECT * FROM amazonReviews LIMIT 5", con)

预期:

    reviewerID  asin    reviewerName    reviewText  overall summary unixReviewTime  reviewTime  helpful total
0   A1EE2E3N7PW666  B000GFDAUG  Aaron L. Allen "Orgazmo1009"    CA Lewsi' review should be removed. he's revie...   5   Stupid  1202256000  02 6, 2008  0   0
1   AGZ8SM1BGK3CK   B000GFDAUG  Mind's Clay I truly love the humor of South Park. It's soc...   5   "More Moist Than Should Be" Humor   1198195200  12 21, 2007 1   1
实际:

    asin    overall reviewText  reviewTime  reviewerID  reviewerName    summary unixReviewTime  helpful total
0   A1EE2E3N7PW666  B000GFDAUG  Aaron L. Allen "Orgazmo1009"    CA Lewsi' review should be removed. he's revie...   5   Stupid  1202256000  02 6, 2008  0   0
1   AGZ8SM1BGK3CK   B000GFDAUG  Mind's Clay I truly love the humor of South Park. It's soc...   5   "More Moist Than Should Be" Humor   1198195200  12 21, 2007 1   1[enter image description here][1]

如果表已经存在,并且列的结果顺序为“实际”的话,就会发生这种情况。由于“创建”sql中的“如果不存在”,将不会创建表。由于清单类型,数据将在没有“类型”投诉的情况下加载