我将csv文件导入Python并尝试使用SQLLite。但是当我创建表时,列和值都被更改了。例如,在overall列中出现值“asin”。reviewerID列接收到值“overall”,以此类推。 如何修复
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]
如果表已经存在,并且列的结果顺序是“实际的”,则会发生这种情况。由于“CREATE”sql中的“IF not EXISTS”,将不会创建表。由于清单类型的原因,数据将在没有“类型”投诉的情况下加载
相关问题 更多 >
编程相关推荐