使用python psycopg2将csv数据插入postgres数据库时出现零长度分隔符错误
我正在尝试使用psycopg2将一些csv文件的内容加载到我的postgres数据库中。运行脚本时,出现以下错误: psycopg2.errors.SyntaxError:在或处以零长度分隔的标识符 在“附近” 我知道该错误很可能是由于“example”的空字符串值周围的单引号引起的,但我不知道这会导致问题的原因使用python psycopg2将csv数据插入postgres数据库时出现零长度分隔符错误,python,django,postgresql,psycopg2,delimiter,Python,Django,Postgresql,Psycopg2,Delimiter,我正在尝试使用psycopg2将一些csv文件的内容加载到我的postgres数据库中。运行脚本时,出现以下错误: psycopg2.errors.SyntaxError:在或处以零长度分隔的标识符 在“附近” 我知道该错误很可能是由于“example”的空字符串值周围的单引号引起的,但我不知道这会导致问题的原因 df = pandas.read_csv(cip_location, header=0, encoding='ISO-8859-1', dtype=str)
df = pandas.read_csv(cip_location, header=0, encoding='ISO-8859-1', dtype=str)
number_loaded_rows += len(df.index)
for index, row in df.iterrows():
row = row.squeeze()
cip_code = row['CIPCode']
cip_code = cip_code[cip_code.find('"') + 1:cip_code.rfind('"')]
if cip_code.startswith('0'):
cip_code = cip_code[1:]
cip_title = row['CIPTitle']
cip_def = row['CIPDefinition']
exam_string = row['Examples']
exam_string = exam_string.replace('Examples:', '').replace(' - ', '').replace(' -', '')
examples = exam_string
cip_codes[cip_code] = {
'code': cip_code,
'title': cip_title,
'definition': cip_def,
'examples': examples
}
with gzip.GzipFile(ending_location, 'r') as f:
bytes = f.read()
string = bytes.decode('utf-8')
loaded_unis = jsonpickle.decode(string)
print('Finished loading in ' + str(time.time() - start_load))
import psycopg2
cnx = psycopg2.connect('host=localhost dbname=postgres user=postgres password=password')
count = 0
cursor = cnx.cursor()
for d in cip_codes.values():
print('Inserted: %s' % count)
print('Trying to insert (%s, "%s", "%s", "%s");' % (d['code'], d['title'], d['definition'], d['examples']))
cursor.execute('CALL InsertCIP(%s, "%s", "%s", "%s");' % (str(d['code']), d['title'].replace('"', "'"),
d['definition'].replace('"', "'"),
d['examples'].replace('"', "'")))
count = count + 1
cnx.commit()
cursor.close()
cnx.close()
Gzip代码似乎没有做任何与sql相关的事情 在这种情况下,数据中的第一行似乎为空,转义导致数据的第四列为“” 试着让psycopg2帮你逃走。 如果您有大量数据,
excute\u batch
比在每行上循环更快
data = [tuple(r) for r in cip_codes.values]
cursor = cnx.cursor()
insert_sql = """InsertCIP(%s, %s, %s, %s)"""
execute_batch(cur, insert_sql, data, page_size=1000 )
cursor.commit()
cursor.close()
希望这能有所帮助,我不确定insertchip是什么样子