Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams constr = "dbname='mydb' user= 'pgsql' host='127.0.0.1'" db = psycopg2.connect(constr) st = db.cursor() #st.copy_from(f_cm, 'mytable', sep='","', columns = ('col1','col2', 'col3')) #instead of st.copy_from(f_cm, 'mytable', sep=',', columns = ('col1','col2', 'col3'))

Date format is:

"54654","4454","45465"
"54546","4545","885dds45"
"54536","4546","885dd45"

I have searched and found Good news at psycopg New in psycopg2.0.9

Go to heading What’s new in psycopg 2.0.9¶ which states: copy_from() and copy_to() can now use quoted separators.

tools:

psycopg2 = 2.4.5
python = 2.7.3

Seems Like cursor.copy_from or copy_to does not support Quoted sheets. solution is to use copy_expert.

import psycopg2
f_cm = open('cm.sql', 'r')
constr = "dbname='mydb' user= 'pgsql' host='127.0.0.1'"
db = psycopg2.connect(constr)
st = db.cursor()
copy = "COPY mytable(col1,col2, col3) FROM STDIN with csv"
st.copy_expert(sql=copy, file=f_cm)
db.commit()
st.close()
db.close()
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.