0

I know that I can make a simple in-memory CSV file like this:

from cStringIO import StringIO
tsv_string = '1\t2\t3\n'
f = StringIO(tsv_string)

But how do I adapt this to include null / None values? I am asking because I am generating my tsv_string dynamically using real data. Some of the integer fields are null and I cannot replace them with an empty string since Postgres cannot interpret that correctly.

To give even more background, I am doing this so that I can use psycopg2's copy_from function which currently gives me this error due to the reason above:

psycopg2.DataError: invalid input syntax for integer: ""

So the question is, how do I place null values in my in-memory TSV file. Is there a more suitable file extension to use?

1 Answer 1

1

I found my answer from this question.

I had to specify the null argument as an empty string:

cursor.copy_from(f, 'my_table', columns=columns, null='')

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.