请问使用pycharm运行此代码怎么没有输出

借问使用pycharm运维此代码怎么未有出口展现C:UserszcPycharmProjectsuntitled2venvScriptspython.exeC:/Users/zc/PycharmProjects/untitled8/sj.py进度已甘休,退出代码0importpymysqlimportxlrdimportsysdefmysql_link(de_name):try:db=pymysql.connect(host="127.0.0.1",user="root",passwd="love1111",db=mysql,charset='utf8')returndbexcept:print("couldnotconnecttomysqlserver")'''读取excel函数args:excel_filereturns:book'''defopen_excel(excel_file):try:book=xlrd.open_workbook('3699969Q.xls')#文件名,把文件与py文件放在同等目录下print(sys.getsizeof(book卡塔尔(英语:State of Qatar)卡塔尔returnbookexcept:print("openexcelfilefailed!")defstore_to(db_name,table_name,excel_file):db=mysql_link(db_name)#开辟数据库连接cursor=db.cursor(卡塔尔(قطر‎#动用cursor(卡塔尔(قطر‎方法创设三个游标对象cursorlist=[]#概念列表用来寄放数据table_name='dsqj'book=open_excel(excel_file)#打开excel文件sheets=book.sheet_names()#收获具备sheet表名forsheetinsheets:sh=book.sheet_by_name(sheet)#开发每一张表row_num=sh.nrowsprint(row_num)num=0#用来决定每一遍插入的数量foriinrange(1,row_num):#先是行是标题名,对应表中的字段名所以应该从第二行开端,Computer以0初阶计数,所以值是1row_data=sh.row_values(i)#按行获取excel的值value=(row_data[0],row_data[1],row_data[2],row_data[3],row_data[4],row_data[5],row_data[6],row_data[7],row_data[8],row_data[9],row_data[10],row_data[11],row_data[12],row_data[13],row_data[14])list.append(value)#将数据暂存在列表num =1if(num=10卡塔尔国:#每黄金时代万条数据奉行二次插入print(sys.getsizeof(list卡塔尔(قطر‎卡塔尔(قطر‎sql="INSERTINTO" table_name "(jgh,cz,kh,zzh,zh,sfz,khrqdqrq,je,xhje,xhrq)VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"cursor.executemany(sql,list)#执行sql语句num=0#计数归零list.clear(卡塔尔国#清空listsql="INSERTINTO" table_name "(jgh,cz,kh,zzh,zh,sfz,khrqdqrq,je,xhje,xhrq)VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"cursor.executemany(sql,list)#执行sql语句print("worksheets:" sheet "hasbeeninserted" len(list) "datas!")list.clear()#清空listprint("worksheets:" sheet "hasbeeninserted" str(row_num) "datas!")db.commit()#提交cursor.close()#关门连接db.close(卡塔尔国

python意况的装置配备就背着了,个人爱好pydev的付出意况。

做事中有个excel,在那之中一列是意气风发组数字。数字的含义是货品的商品码。商品的情况有3种,1,2,3.急需连接纳后台的oracle数据库,查询商品的景况,然后从这么些excel表的商品码中筛选出1要么2的。然后保留在其它二个excel里。

python拆解剖判excel须求动用第三方的库,这里选用选用xlrd

整个代码是:

先看excel内容:

#coding:utf-8
import xlrd
import xlwt

 

import cx_Oracle
db=cx_Oracle.connect('用户名','密码','ip:1521/服务名')

下一场是转换的数据库:

workbook = xlwt.Workbook(encoding = 'ascii')
worksheet = workbook.add_sheet('My Worksheet')

 

data = xlrd.open_workbook('1.xls')
table = data.sheets()[0]
nrows = table.nrows

下边是源代码:

for i in range(nrows):
      rowi=unicode(table.row_values(i)[0])
      sql="select * from tskuplu where pluCode = '" rowi "' And IsActive = '1' and YwStatus in ('1','2')"
      cr=db.cursor()
      cr.execute(sql)
      rs=cr.fetchall()
      if rs:
             print rs[0][1],type(rs[0][1])
             worksheet.write(i, 0, label = rs[0][1])
             workbook.save('111.xls')

[python]
#!/usr/bin/python  
# encoding=utf-8  
 
'''''
Created on 2013-4-2
 
@author: ting
''' 
from xlrd import open_workbook 
import sqlite3 
import types 
 
def read_excel(sheet): 
    # 判别有效sheet  
    if sheet.nrows > 0 and sheet.ncols > 0: 
        for row in range(1, sheet.nrows): 
            row_data = [] 
            for col in range(sheet.ncols): 
                data = sheet.cell(row, col).value 
                # excel表格内容数据类型调换  float->int,unicode->utf-8  
                if type(data) is types.UnicodeType: data = data.encode("utf-8") 
                elif type(data) is types.FloatType: data = int(data) 
                row_data.append(data) 
            check_data_length(row_data) 
 
# 检查row_data长度  
def check_data_length(row_data): 
    if len(row_data) == 3: 
        insert_sqlite(row_data) 
 
def insert_sqlite(row_data): 
    # 张开数据库(荒诞不经时会创造数据库)  
    con = sqlite3.connect("test.db") 
    cur = con.cursor() 
    try: 
        cur.execute("create table if not exists contacts(_id integer primary key " 
                       "autoincrement,name text,age integer,number integer)") 
        # 插入数据毫无使用拼接字符串的格局,轻便选取sql注入攻击  
        cur.execute("insert into contacts(name,age,number) values(?,?,?)", row_data) 
        con.commit() 
    except sqlite3.Error as e: 
        print "An error occurred: %s", e.args[0] 
    finally: 
        cur.close 
        con.close 
 
 
xls_file = "test.xls" 
book = open_workbook(xls_file) 
 
for sheet in book.sheets(): 
    read_excel(sheet) 
print "------ Done ------" 

cr.close()

#!/usr/bin/python
# encoding=utf-8

db.close()

'''
Created on 2013-4-2

仿照效法资料:

@author: ting
'''
from xlrd import open_workbook
import sqlite3
import types

def read_excel(sheet):
    # 推断有效sheet
    if sheet.nrows > 0 and sheet.ncols > 0:
        for row in range(1, sheet.nrows):
            row_data = []
            for col in range(sheet.ncols):
                data = sheet.cell(row, col).value
                # excel表格内容数据类型转变  float->int,unicode->utf-8
                if type(data) is types.UnicodeType: data = data.encode("utf-8")
                elif type(data) is types.FloatType: data = int(data)
                row_data.append(data)
            check_data_length(row_data)

# 检查row_data长度
def check_data_length(row_data):
    if len(row_data) == 3:
        insert_sqlite(row_data)

def insert_sqlite(row_data):
    # 展开数据库(不设不经常会创设数据库)
    con = sqlite3.connect("test.db")
    cur = con.cursor()
    try:
        cur.execute("create table if not exists contacts(_id integer primary key "
                       "autoincrement,name text,age integer,number integer)")
        # 插入数据并不是选择拼接字符串的方法,轻便摄取sql注入攻击
        cur.execute("insert into contacts(name,age,number) values(?,?,?)", row_data)
        con.commit()
    except sqlite3.Error as e:
        print "An error occurred: %s", e.args[0]
    finally:
        cur.close
        con.close

xls_file = "test.xls"
book = open_workbook(xls_file)

for sheet in book.sheets():
    read_excel(sheet)
print "------ Done ------"

 

python剖判excel需求选择第三方的库,这里选用选拔xlrd 先看excel内容: 然后是生成...

本文由新葡萄京娱乐场8522发布于计算机编程,转载请注明出处:请问使用pycharm运行此代码怎么没有输出

TAG标签:
Ctrl+D 将本页面保存为书签,全面了解最新资讯,方便快捷。