Python Forum
python pandas sql table with header
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
python pandas sql table with header
#1
Hi Team,

please help me first time using pandas for extracting sql table with headers into csv file.

how to write below header line to csv.
writer.writerow(col[0] for col in cursor.description)

or any other best way of extracting sql table data with headers. Sep="|" via pandas.

below is my attempted code

import csv
import pyodbc
import pandas as pd

cursor = connection.cursor()
f = open('output.csv', 'w')

cursor.execute('select * from mydata')
with open("output.csv", "w", newline="") as outfile:
    writer = csv.writer(outfile, delimiter="|", quoting=csv.QUOTE_NONNUMERIC)
    writer.writerow(col[0] for col in cursor.description)    
# Get data in batches
    while True:
        df = pd.DataFrame(cursor.fetchmany(1000))
        if len(df) == 0:
            break
        else:
            df.to_csv(f, index=False, header=False, mode='a',sep="|")

    # Clean up
f.close()
cursor.close()
connection.close()
Reply
#2
I can't test this as I don't have a copy of your database, but should be very close if not entirely correct:
It's a lot simpler than you realise.

import pandas as pd
import sqlite3

conn = sqlite3.connect(, isolation_level=None,
    detect_types=sqlite3.PARSE_COLNAMES)
df = pd.read_sql_query("SELECT * FROM mydata", conn)
df.to_csv('output.csv', index=False)
Reply
#3
Hi Larz60,

thanks for help need still more help, I am using Microsoft SQL server


1) is sqlite3 Support for extracting data from Microsoft SQL Server.[/b]
2) I want to extract sql data in chunk size , Want to avoid memory full issue. SQL Tables are big in Big in size
3) I want data to be written in CSV with delimiter "|"



import pandas as pd
import sqlite3

conn = sqlite3.connect(, isolation_level=None,
    detect_types=sqlite3.PARSE_COLNAMES)
df = pd.read_sql_query("SELECT * FROM mydata", conn)
df.to_csv('output.csv', index=False)
Reply
#4
mg24 Wrote:1) is sqlite3 Support for extracting data from Microsoft SQL Server.[/b]
No, sqlite3 is only for sqlite database.
I don't use SQL Server, but connection should be similar.

mg24 Wrote:2) I want to extract sql data in chunk size , Want to avoid memory full issue. SQL Tables are big in Big in size
see: https://stackoverflow.com/a/29522443

mg24 Wrote:3) I want data to be written in CSV with delimiter "|"
pandas calls the delimiter 'sep' so look for this in the docs.
https://pandas.pydata.org/pandas-docs/st...o_csv.html

change: df.to_csv('output.csv', index=False)
to: df.to_csv('output.csv', sep='|', index=False)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Trying to get counts/sum/percentages from pandas similar to pivot table cubangt 6 1,427 Oct-06-2023, 04:32 PM
Last Post: cubangt
  Using pyodbc&pandas to load a Table data to df tester_V 3 830 Sep-09-2023, 08:55 PM
Last Post: tester_V
  Python create a spreadsheet with column and row header ouruslife 4 1,648 Jul-09-2022, 11:01 AM
Last Post: Pedroski55
  Insert a multiple constant value after header in csv file using python shantanu97 1 1,159 Apr-24-2022, 10:04 AM
Last Post: Pedroski55
  Sum the values in a pandas pivot table specific columns klllmmm 1 4,652 Nov-19-2021, 04:43 PM
Last Post: klllmmm
  pandas pivot table: How to find count for each group in Index and Column JaneTan 0 3,323 Oct-23-2021, 04:35 AM
Last Post: JaneTan
  Yahoo_fin, Pandas: how to convert data table structure in csv file detlefschmitt 14 7,807 Feb-15-2021, 12:58 PM
Last Post: detlefschmitt
  Convert table in pandas tgottsc1 2 2,000 Jan-26-2021, 01:58 PM
Last Post: tgottsc1

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020