0

Making list of all tables in public schema and trying to insert row count in csv file , all things are working file but data is not inserting on continues row its skiping row

import psycopg2
import csv
conn=psycopg2.connect(host="localhost",database="practice_db",user="postgres",
                  password="admin")
print('connected')
cursor=conn.cursor()
cursor.execute("select version()")
print(cursor.fetchall())


sql="select table_name from information_schema.tables where table_type='BASE TABLE' AND TABLE_SCHEMA='public'"
cursor.execute(sql)
list_table=[item[0] for item in cursor.fetchall()]
print(list_table)
with open('python_count_psql.csv','w') as file:
       writer=csv.writer(file)
       writer.writerow(['Table','Count'])
       for row in list_table:
             cursor.execute('SELECT COUNT(*) FROM {}'.format(row))
             writer.writerow([row,cursor.fetchone()[0]])  

enter image description here

zeeshan12396
  • 382
  • 1
  • 8
  • Possible duplicate of https://stackoverflow.com/questions/3191528/csv-in-python-adding-an-extra-carriage-return-on-windows – cogitoergosum Jul 19 '22 at 14:59
  • The examples for the `reader` and `writer` here [CSV](https://docs.python.org/3/library/csv.html#module-csv) address the new line issue and how to deal with it. – Adrian Klaver Jul 19 '22 at 16:58

0 Answers0