-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsertToDatabase.py
More file actions
51 lines (38 loc) · 1.15 KB
/
Copy pathinsertToDatabase.py
File metadata and controls
51 lines (38 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env python
# coding:utf8
# 将清洗的数据插入数据库
import MySQLdb
import MySQLdb.cursors
inputFile = 'douban_movie_clean.txt'
fr = open(inputFile, 'r')
firstLine = True
db = MySQLdb.connect(host='localhost', user='root', passwd='', db='douban', port=3306, charset='utf8', cursorclass = MySQLdb.cursors.DictCursor)
db.autocommit(True)
cursor = db.cursor()
count = 0
for line in fr:
if firstLine:
firstLine = False
continue
line = line.split('^')
movieId= line[0]
title = line[1]
url = line[2]
cover = line[3]
rate = line[4]
director = line[5]
composer = line[6]
actor = line[7]
category = line[8]
district = line[9]
language = line[10]
showtime = line[11]
length = line[12]
othername = line[13]
description = line[14]
cursor.execute('insert into movie(movieId,title,url,cover,rate,director,composer,actor,category,district,language,showtime,length,othername,description) values(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)',[movieId,title,url,cover,rate,director,composer,actor,category,district,language,showtime,length,othername,description])
count = count + 1
print count, title
fr.close()
db.close()
cursor.close()