MYSQL 데이터베이스에 CSV 파일을 가져 오려고하지만 절대 나타나지 않습니다! 내가 뭔가를 엉망으로 만든다면 잘 모르겠지만 너희들은 좀 봐야 해.CSV 파일을 MySQL 데이터베이스에 삽입하면 삽입이 작동하지 않습니다.
# its time for fetching data :)
import csv
import urllib2
path = 'C:\\Users\\User\\Desktop\\Downloads\\'
response = urllib2.urlopen('https://files.ontario.ca/opendata/births_by_year_and_month_per_place_of_event_city_2010_0.csv')
html = response.read()
file=open(path +"births.CSV", "r")
reader = csv.reader(file)
for row in reader:
print row
#Server Connection to MySQL:
import MySQLdb
conn = MySQLdb.connect(host= "localhost",
user="root",
passwd="admin",
db="cool")
x = conn.cursor()
try:
x.execute("""INSERT INTO import (test) VALUES (%s,%s,%s,%s)""",(row))
conn.commit()
except:
conn.rollback()
conn.close()
종료 코드 0으로 끝나고 모든 것을 인쇄하지만 결코 데이터베이스에 삽입되지 않습니다. 다음은 %의 부분이 함께 할 수있는 뭔가가 아닌 경우 확인 인쇄
['2010', 'Sep', 'THUNDER BAY', '132']
['2010', 'Sep', 'TIMMINS', '59']
['2010', 'Sep', 'TORONTO', '3412']
['2010', 'Sep', 'WELLAND', '58']
['2010', 'Sep', 'WELLINGTON NORTH', '6']
['2010', 'Sep', 'WINDSOR', '337']
['2010', 'Sep', 'WOODSTOCK', '50']
Process finished with exit code 0
편집 임 샘플입니다,하지만 난 도움이 필요합니다. 감사!
편집 2 위대한! 여러분을 도와 주신 모든 분들께 감사드립니다. 하지만 지금은 새로운 편집이 있는데, 나는 잘못되어있는 것을 발견하는 것 같습니다. 여기에 새 코드가 있습니다!
import csv
import urllib2
path = 'C:\\Users\\hongyu\\Desktop\\Downloads\\'
response = urllib2.urlopen('https://files.ontario.ca/opendata/births_by_year_and_month_per_ place_of_event_city_2010_0.csv')
html = response.read()
file=open(path +"births.CSV", "r")
reader = csv.reader(file)
#Server Connection to MySQL:
import MySQLdb
conn = MySQLdb.connect(host= "localhost",
user="root",
passwd="admin",
db="cool")
for row in reader:
x = conn.cursor()
try:
x.execute("""INSERT INTO testing (year, month, city, num) VALUES (%s,%s,%s,%s)""", (row))
conn.commit()
except :
conn.rollback()
print(row)
print("This row failed to insert")
conn.close()
이제 코드를 실행하면 어떤 행을 삽입하지 못한 것인지 알 수 있습니다. 그러나 많은 행이 실패했습니다. 여기에 약간의 결과가 있습니다.
['1994', 'Dec', 'KAPUSKASING', '10']
This row failed to insert
['1994', 'Dec', 'KENORA', '29']
This row failed to insert
['1994', 'Dec', 'KINCARDINE', '11']
This row failed to insert
['1994', 'Dec', 'KINGSTON', '198']
This row failed to insert
['1994', 'Dec', 'KIRKLAND LAKE', '13']
This row failed to insert
['1994', 'Dec', 'KITCHENER', '363']
This row failed to insert
['1994', 'Dec', 'LEAMINGTON', '31']
This row failed to insert
어떤 도움이 필요합니까? 데이터베이스에 거의 모든 것이 누락되어 있으며 여기에 약간 있습니다. http://prntscr.com/h6dhod P.S : 도시로 시작하는 것뿐만 아니라 토론토도 있습니다.
에 $ 항목 [0], $ 항목을 사용할 수 있지만, 당신은 단지 열에서 1 열 이름이 명부. – Barmar
오류 메시지를'except :'블록에 출력 해보십시오. – Barmar
어떻게 오류 메시지를 인쇄합니까? – FireBlaster