我正在使用Python將Excel導入到PostgreSQL中。以下是我遇到問題的領域。實際銷售價格是一個整數數據類型的值,有一段時間,但現在這個列包含一個N/A值,它正在炸燬我的Python腳本。有什麼我可以添加到這個腳本,它會告訴它在不將數據類型更改爲varchar的情況下引入N/A。N/A在整數字段
import psycopg2
import xlrd
book = xlrd.open_workbook("T:\DataDump\8888.xlsx")
sheet = book.sheet_by_name("ProjectConsolidated")
database = psycopg2.connect (database = "***", user="****")
cursor = database.cursor()
delete = """drop table if exists "Python".ProjectConsolidated"""
print (delete)
mydata = cursor.execute(delete)
cursor.execute('''CREATE TABLE "Python".ProjectConsolidated
(DCAD_Prop_ID VARCHAR(25),
Actual_Close_Date date,
Actual_Sales_Price integer,
);''')
print "Table created successfully"
query = """INSERT INTO "Python".ProjectConsolidated (DCAD_Prop_ID,
Actual_Close_Date, Actual_Sales_Price)
VALUES (%s, %s, %s)"""
for r in range(1, sheet.nrows):
DCAD_Prop_ID = sheet.cell(r,0).value
Actual_Close_Date = None if not sheet.cell(r,1).value else xlrd.xldate.xldate_as_datetime(sheet.cell(r,1).value,book.datemode)
Actual_Sales_Price = None if not sheet.cell(r,2).value else sheet.cell(r,2).value
values = (DCAD_Prop_ID,
Actual_Close_Date, Actual_Sales_Price)
cursor.execute(query, values)
cursor.close()
database.commit()
database.close()
print ""
print "All Done! Bye, for now."
print ""
columns = str(sheet.ncols)
rows = str(sheet.nrows)
print "I just imported Excel into postgreSQL"
你想如何在Postgres數據庫中代表Excel中的N/A?空白字符串?空值? 0? –
無論如何,我可以將N/A作爲一個值而不改變數據類型嗎? –
你可以發佈你的腳本或更完整地解釋這個問題嗎? –