+1 vote
in Databases by (88.7k points)

I created a zipped TSV file using the following SQL:

\copy (select * from concept_relationship) to 'concept_relationship.tsv.gz'

But the following python code says bad zipped file. How to fix this issue.

with gzip.open('concept_relationship.tsv.gz','rt') as fin:

1 Answer

+2 votes
by (15.9k points)
selected by
 
Best answer

The issue is that the \copy command in PostgreSQL does not create a proper gzip-compressed file, even if you specify a .gz extension in the filename. Instead, it creates a plain text file with the .gz extension, which cannot be opened as a gzip-compressed file.

To fix this issue, you need to properly compress the file after exporting it with the \copy command. 

Here are the steps:

  • Run your original \copy command without the .gz extension (export as a plain TSV file):

\copy (select * from concept_relationship) to 'concept_relationship.tsv'

  • Compress the TSV file using the gzip command:

gzip concept_relationship.tsv

This will create the concept_relationship.tsv.gz file, which can be properly read by Python's gzip.open() function.

Related questions

+1 vote
1 answer
+1 vote
1 answer
+5 votes
1 answer
asked Oct 14, 2021 in Programming Languages by praveen (74.8k points)
+2 votes
1 answer

...