python - CSV - 使用 Python 的 MYSQL

标签 python mysql csv

在阅读了几个输入后,我仍然无法让它工作。 很可能我做错了,但我尝试了几种不同的方法

我想做的是从 CSV 中提取数据并将其添加到我新创建的数据库/表中

我的 csv 输入看起来像这样

NodeName,NeId,Object,Time,Interval,Direction,NeAlias,NeType,Position,AVG,MAX,MIN,percent_0-5,percent_5-10,percent_10-15,percent_15-20,percent_20-25,percent_25-30,percent_30-35,percent_35-40,percent_40-45,percent_45-50,percent_50-55,percent_55-60,percent_60-65,percent_65-70,percent_70-75,percent_75-80,percent_80-85,percent_85-90,percent_90-95,percent_95-100,IdLogNum,FailureDescription
X13146PAZ,5002,1/11/100,2016-05-16 00:00:00,24,Near End,GE0097-TN01.1,AMM 20PB,-,69684,217287,772,10563,8055,10644,15147,16821,13610,7658,2943,784,152,20,3,0,0,0,0,0,0,0,0,0,-
...
X13146PAZ,5002,1/11/102,2016-05-16 00:00:00,24,Near End,GE0097-TN01.1,AMM 20PB,-,3056,28315,215,86310,90,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-
...
X13146PAZ,5002,1/11/103,2016-05-16 00:00:00,24,Near End,GE0097-TN01.1,AMM 20PB,-,769,7195,11,86400,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-

mysql 表已创建,但可能是问题所在,因为一些 ar varchar 列和一些是整数列

如果有用的话,我的服务器是 Ubuntu

我的代码

        # -*- coding: utf-8 -*-
#Imports
from datetime import date, timedelta
import sys
import MySQLdb as mdb
import csv
import os

#Vars
Yesterday = date.today() - timedelta(1)

#Opening document
RX_Document = open('./reports/X13146PAZ_TN_WAN_ETH_BAND_RX_' + Yesterday.strftime("%Y%m%d") + "_231500.csv" , 'r')
RX_Document_Str = './reports/X13146PAZ_TN_WAN_ETH_BAND_RX_' + Yesterday.strftime("%Y%m%d") + "_231500.csv"

csv_data = csv.reader(file(RX_Document_Str))

con = mdb.connect('localhost', 'username', 'password','tn_rx_utilization');


counter = 0
for row in csv_data: 
    if counter == 0:
        print row
        continue


    counter = 1
    if counter == 1:
        cur = con.cursor()
        cur.execute('INSERT INTO RX_UTIL(NodeName, NeId, Object, Time, Interval1,Direction,NeAlias,NeType,Position,AVG,MAX,MIN,percent_5-10,percent_10-15,percent_15-20,percent_20-25,percent_25-30,percent_30-35,percent_35-40,percent_40-45,percent_45-50,percent_50-55,percent_55-60,percent_60-65,percent_65-70,percent_70-75,percent_75-80,percent_80-85,percent_85-90,percent_90-95,percent_95-100,IdLogNum,FailureDescription)' 'VALUES("%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s")',tuple(row[:34]))
        con.commit()

        #cur.execute("SELECT VERSION()")

        #ver = cur.fetchone()




con.commit()
con.close()

最佳答案

您不应将占位符 %s 放在引号 " 中:

cur.execute('''INSERT INTO RX_UTIL(NodeName, NeId, Object, Time, Interval1,Direction,
    NeAlias,NeType,Position,AVG,MAX,MIN,"percent_5-10","percent_10-15",
    "percent_15-20","percent_20-25","percent_25-30","percent_30-35",
    "percent_35-40","percent_40-45","percent_45-50","percent_50-55",
    "percent_55-60","percent_60-65","percent_65-70","percent_70-75",
    "percent_75-80","percent_80-85","percent_85-90","percent_90-95",
    "percent_95-100",IdLogNum,FailureDescription)
    VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,
        %s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)''', tuple(row[:33]))

关于python - CSV - 使用 Python 的 MYSQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37282193/

相关文章:

Python 将多个对象加载到映射中以在内存缓存中使用

php - 如何使用 PHP/MySQL 添加日期?

mysql - 将一个表插入另一个具有冲突的自动增量值的最简单方法?

上传后 CSV 文件中的 PHP 格式编号

R 中的 Python zip 模式

python - 为什么在 Python 中返回 'NoneType'?

python - 哪个性能更好 : len(a[:-1]) or len(a)-1

php - PDO INSERT ON DUPLICATE KEY UPDATE 仅插入和更新 6 行

python - 如何使用Python循环删除包含特定单词的行?

csv - 在 pig 中存储多个变量