XLRD中的字符替换(。代表-)

时间:2019-11-07 08:33:01

标签: python python-3.x xlrd

我在用1个列中的xlrd替换1个字符时遇到问题。 此字符是。我要替换为- xlmx文件中的数据为(。****),需要为(--****) 所以我用xlrd导入了de xlmx,但是在完成后我不知道如何更改列中条纹的点,它将直接将文件导入MYSQL。

我有以下代码。

import xlrd, xlwt
import pymysql as MySQLdb

# Open the workbook and define the worksheet
oldbook = xlrd.open_workbook("file location")
wb = copy(oldbook)
sheet = oldbook.sheet_by_index(0)

# Source

# Establish a MySQL connection
database = MySQLdb.connect (host="localhost", user = "root", passwd = "", db = "mydb")

# Get the cursor, which is used to traverse the database, line by line
cursor = database.cursor()

# Create the INSERT INTO sql query
query = """INSERT INTO leden (lidid, name, date) VALUES (%s, %s, %s)"""

# Create a For loop to iterate through each row in the XLS file, starting at row 2 to skip the headers

for r in range(1, sheet.nrows):
        lidid        = sheet.cell(r,1).value
        name    = sheet.cell(r,2).value
        date                = sheet.cell(r,4).value
        # Assign values from each row
        values = (lidid, name, date)

        # Execute sql Query
        cursor.execute(query, values)

# Close the cursor
cursor.close()

# Commit the transaction
database.commit()

# Close the database connection
database.close()

0 个答案:

没有答案