使用python获取错误ORA-01722从csv文件向oracle插入数据时:无效数字

时间:2017-08-02 20:36:24

标签: python oracle csv oracle11g

我正在尝试将数据加载到oracle表。 here是我之前的问题,我已经发布了整个代码,我是如何做到这一点的。

这是代码:

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")

所以我纠正了在回答我的问题时建议的所有过程,以便错误现在已经解决,我收到了这个新错误ORA-01722: invalid number

当我使用导入选项将数据直接加载到oracle时,数据被加载。当我试图在此代码中读取相同的文件并尝试推送数据时,我收到此错误。

我打印行[:1]和行[: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

有人可以指出我在代码中所做的错误

1 个答案:

答案 0 :(得分:1)

您的csv文件中有一个标题行,它不是数字数据的一部分。在将next(reader)映射到文件句柄之后,您必须使用csv.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
相关问题