簡體   English   中英

使用python將數據從csv文件插入到oracle時出現錯誤ORA-01722:無效編號

[英]while inserting data to oracle from csv file using python getting error ORA-01722: invalid number

我正在嘗試將數據加載到oracle表。 是我以前的問題,我在其中發布了整個代碼,我該怎么做。

這是代碼:

def Create_list():
reader = csv.reader(open("Query_result_combined.csv","r"))
lines=[]
print("Creating a list")
for line in reader:
    lines.append(line)
return lines


def Insert_data():
#connection logic goes here 
print("Connecting Now!!")
#conn = cx_Oracle.connect(connstr)
con = cx_Oracle.connect(db_user,db_password,db_connection_name)
print("Connected to Oracle!!")
lines=Create_list()
cur=con.cursor()
print("Inserting data")
for line in lines:
    cur.execute("INSERT INTO A608232_QUERY_RESULT (InteractionId,QueryId,Score,StartOffsetInMs,EndOffsetInMs,SpeakerRole,QueryIdentity,SpeakerId) VALUES(:1,:2,:3,:4,:5,:6,:7,:8)",line)
con.commit ()
cur.close()
print("completed")

因此,我更正了回答我的問題時建議的所有proproches,以便現在我收到此新錯誤ORA-01722: invalid number ,錯誤已得到解決。

當我使用import選項將數據直接加載到oracle中時,該數據將被加載。 當我嘗試在此代碼中讀取同一文件並嘗試推送數據時,出現此錯誤。

我打印了lines [:1]和lines [:2],這是我得到的輸出:

[['InteractionId', 'QueryId', 'Score', 'StartOffsetInMs', 'EndOffsetInMs', 
'SpeakerRole', 'QueryIdentity', 'SpeakerId']]
[['InteractionId', 'QueryId', 'Score', 'StartOffsetInMs', 'EndOffsetInMs', 
'SpeakerRole', 'QueryIdentity', 'SpeakerId'], ['34118470', '27', '45.63345', 
'89900', '90980', 'U', 'e54fd492-8877-4534-997b-9dbe9a8fbd74', '']]
 Inserting data

有人可以指出我在代碼中所做的錯誤

您的csv文件中有一個標題行,它不是數字數據的一部分。 您必須在將csv.reader映射到文件句柄之后,才使用next(reader)跳過它。

另外:使用上下文管理器確保文件將被關閉,並且不需要循環,只需將行迭代器轉換為列表(當第一行被跳過時)以讀取所有行(會更快)

def Create_list():
    with open("Query_result_combined.csv","r") as f:
       reader = csv.reader(f)
       next(reader)  # skips title line
       return list(lines)  # directly iterate on the rest of the lines

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM