{"id":22957,"date":"2024-01-08T06:36:00","date_gmt":"2024-01-08T05:36:00","guid":{"rendered":"https:\/\/expert-only.com\/?p=22957"},"modified":"2024-02-23T15:50:30","modified_gmt":"2024-02-23T14:50:30","slug":"how-to-export-a-sql-server-table-into-a-csv-file-in-python","status":"publish","type":"post","link":"https:\/\/expert-only.com\/en\/python\/how-to-export-a-sql-server-table-into-a-csv-file-in-python\/","title":{"rendered":"How To Export A SQL Server Table Into A CSV File In Python?"},"content":{"rendered":"\n<h4 class=\"wp-block-heading has-text-align-left\"><em>Scripts examples using Python code to export SQL Server data from a table to a CSV file.<\/em><\/h4>\n\n\n\n<p>In this tutorial, we learn how to export data from a SQL Server table into a CSV file using only Python scripts and two dedicated python modules named pyodbc and csv. At the end of this relatively this relatively short and straightforward programming tutorial, you will know how to export data from a SQL Server database to a CSV file using Python. But first of course, you need a sample SQL table to export.<\/p>\n\n\n\n<h5 class=\"wp-block-heading\" id=\"h-the-4-main-steps-to-export-data-from-sql-server-to-csv\"><strong>The 4 main steps to export data from SQL Server to CSV<\/strong><\/h5>\n\n\n\n<ol class=\"wp-block-list\">\n<li>First one is to create a sample table in SQL Server using <a href=\"https:\/\/expert-only.com\/en\/dba\/\"><strong>SSMS<\/strong><\/a><\/li>\n\n\n\n<li>The second one is to connect to the SQL instance.<\/li>\n\n\n\n<li>Then retrieve data from the SQL Server table using a <a href=\"https:\/\/expert-only.com\/en\/t-sql\/sql-server-select-queries-to-filter-data\/\"><strong>basic SQL select statement<\/strong><\/a>.<\/li>\n\n\n\n<li>And export the content of the table to a CSV file using the <a href=\"https:\/\/docs.python.org\/3\/library\/csv.html\" target=\"_blank\" rel=\"noreferrer noopener\">csv library<\/a> in Python.<\/li>\n<\/ol>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 ez-toc-wrap-center counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/expert-only.com\/en\/python\/how-to-export-a-sql-server-table-into-a-csv-file-in-python\/#1-create-a-sample-sql-server-table-to-export\" >1. Create a sample SQL Server table to export<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/expert-only.com\/en\/python\/how-to-export-a-sql-server-table-into-a-csv-file-in-python\/#2-connect-to-sql-server-using-python-and-pyodbc\" >2. Connect to SQL Server using Python and pyodbc<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/expert-only.com\/en\/python\/how-to-export-a-sql-server-table-into-a-csv-file-in-python\/#3-retrieve-the-data-from-the-sql-server-source-table\" >3. Retrieve the data from the SQL Server source table<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/expert-only.com\/en\/python\/how-to-export-a-sql-server-table-into-a-csv-file-in-python\/#4-python-script-to-create-the-csv-file-and-export-data-from-the-table\" >4. Python script to create the CSV file and export data from the table<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/expert-only.com\/en\/python\/how-to-export-a-sql-server-table-into-a-csv-file-in-python\/#5-python-code-example-to-export-sql-server-data-in-a-csv-file\" >5. Python code example to export SQL Server data in a CSV file<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"1-create-a-sample-sql-server-table-to-export\"><\/span>1. Create a sample SQL Server table to export<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>This code creates a sample SQL Server table named <em><strong>dbo.employees<\/strong><\/em> in a database and inserts four sample rows. The table has four columns: id, name, department, and salary. And the <em><strong>id<\/strong><\/em> column is set as <a href=\"https:\/\/expert-only.com\/en\/t-sql\/sql-server-primary-key\/\">the primary key of the table<\/a>. The <em>INSERT INTO<\/em> statements insert four rows of data into the <em><strong>dbo.employees<\/strong><\/em> table. Each row has values for each of the columns in the order they were specified, i.e. : id, name, department, and salary.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"sql\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">CREATE TABLE dbo.employees (\n   id INT PRIMARY KEY,\n   name VARCHAR(50),\n   department VARCHAR(50),\n   salary INT\n);\n\nINSERT INTO employees VALUES (1, 'John Smith', 'Sales', 50000);\nINSERT INTO employees VALUES (2, 'Jane Doe', 'Marketing', 60000);\nINSERT INTO employees VALUES (3, 'Bob Johnson', 'IT', 70000);\nINSERT INTO employees VALUES (4, 'Alice Wong', 'HR', 55000);\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-2-connect-to-sql-server-using-python-and-pyodbc\"><span class=\"ez-toc-section\" id=\"2-connect-to-sql-server-using-python-and-pyodbc\"><\/span>2. Connect to SQL Server using Python and pyodbc<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Indeed, technically the export to CSV file steps are very similar to the <strong><a href=\"https:\/\/expert-only.com\/en\/python\/export-sql-server-table-to-excel-python\/\">Excel export using Python<\/a><\/strong>, but this time the export module used in the next step is different. The first step is to connect to the SQL Server database using the <code><a href=\"https:\/\/pypi.org\/project\/pyodbc\/\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">pyodbc<\/a><\/code> module. In my case i use a SQL Server 2019 installed on a Windows 10 machine. And I use <a href=\"https:\/\/expert-only.com\/en\/ssis\/install-ssis-in-visual-studio-2019\/\">Visual Studio 2019<\/a> to develop my Python scripts. Here is an example code that establishes a connection to a SQL Server database. I use a Windows authentication, so I do not need to enter a user name and a password.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># Import the pyodbc module to manage the odbc connection\nimport pyodbc\n\n# Declare some variables to store the connections details\ndriver = 'SQL Server'\nserver = 'localhost'\ndatabase = 'Expert-Only'\n\n# Connect to the local SQL Server database\nconnection = pyodbc.connect(f'DRIVER={driver};'\n                            f'SERVER={server};'\n                            f'DATABASE={database};'\n                            f'Trusted_Connection=yes;')\n<\/pre>\n\n\n\n<p>Make sure to replace the placeholders with the actual values for your SQL Server connection string: <code>driver<\/code>, <code>server<\/code>, <code>database<\/code>, and if need be username and <code>password<\/code>. In this case the code would be more like this: <\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import pyodbc\n\n# Connect to the SQL Server database\nserver = 'localhost'\ndatabase = 'database_name'\nusername = 'username'\npassword = 'password'\ncnxn = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};'\n                      f'SERVER={server};'\n                      f'DATABASE={database};'\n                      f'UID={username};'\n                      f'PWD={password}')\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-3-retrieve-the-data-from-the-sql-server-source-table\"><span class=\"ez-toc-section\" id=\"3-retrieve-the-data-from-the-sql-server-source-table\"><\/span>3. Retrieve the data from the SQL Server source table<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Once the connection to the SQL Server database is established by the Python code, we can retrieve the data from the table using a T-SQL query. Here is an example code that retrieves all the rows from the <strong><em>dbo.employees<\/em><\/strong> table we created in the first step. Make sure to replace <code>employees<\/code> with the actual name of your SQL Server table, of course.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># Retrieve the data from the employees table\ncursor = connection.cursor()\ncursor.execute('SELECT * FROM dbo.employees')\ndata = cursor.fetchall()\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-4-python-script-to-create-the-csv-file-and-export-data-from-the-table\"><span class=\"ez-toc-section\" id=\"4-python-script-to-create-the-csv-file-and-export-data-from-the-table\"><\/span>4. Python script to create the CSV file and export data from the table<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Finally, we can create a CSV file using the <a href=\"https:\/\/docs.python.org\/3\/library\/csv.html\" target=\"_blank\" rel=\"noreferrer noopener\">csv<\/a> module and write the data to it. Here is an example code that creates a new CSV file named employees.csv and writes the retrieved data to it. Once again,  Make sure to replace the path and name to the file with the actual name that you want to give to your CSV file.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># Import the Python csv module\nimport csv\n\n# Create a new CSV file and write data to it\nwith open(\"C:\\data\\employees.csv\", mode='w', newline='') as file:\n    writer = csv.writer(file)\n    writer.writerows(data)\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-5-python-code-example-to-export-sql-server-data-in-a-csv-file\"><span class=\"ez-toc-section\" id=\"5-python-code-example-to-export-sql-server-data-in-a-csv-file\"><\/span>5. Python code example to export SQL Server data in a CSV file<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>The full Python code example provided below demonstrates all the steps at once. Indeed, the script imports the 2 necessary libraries, i.e. <strong>pyodbc<\/strong> and <strong>csv<\/strong> and sets up a connection to the database. Then it executes a SQL query to select the data. And then writes the data to a new CSV file using the csv library.<\/p>\n\n\n\n<p>The Python code extract the headers from the cursor description and write to the file first, followed by the data itself. Once the export is complete, the program closes the cursor and the database connection. Of course, modify this code is easy, for example to fit specific use case by adjusting the connection string, table name, and file name.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import pyodbc\nimport csv\n\n# Step 1: Connect to the SQL Server instance\n\n# declare some variables to store the connections details\ndriver = 'SQL Server'\nserver = 'localhost'\ndatabase = 'Expert-Only'\n\n# connect to the local SQL Server database\nconnection = pyodbc.connect(f'DRIVER={driver};'\n                            f'SERVER={server};'\n                            f'DATABASE={database};'\n                            f'Trusted_Connection=yes;')\n\n# Step 2: Retrieve data from the SQL Server table\ncursor = connection.cursor()\ncursor.execute('SELECT * FROM dbo.employees')\ndata = cursor.fetchall()\n\n# Step 3: Write data to the CSV file\nwith open(\"C:\\data\\employees.csv\", mode='w', newline='') as file:\n    writer = csv.writer(file)\n    writer.writerow([x[0] for x in cursor.description])  # write header\n    for row in data:\n        writer.writerow(row)\n\n# Step 4: Close the database connection\ncursor.close()\nconnection.close()\n<\/pre>\n\n\n\n<p>The result is a CSV file exported directly under the <strong>C:\\data\\<\/strong> folder, with the same content as the original MS SQL table.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"720\" height=\"640\" src=\"https:\/\/expert-only.com\/wp-content\/uploads\/2023\/03\/python-export-sql-server-table-to-csv-file.jpg\" alt=\"How to export a SQL Server table into a CSV file in Python? The CSV file contains the exported data.\" class=\"wp-image-22985\" srcset=\"https:\/\/expert-only.com\/wp-content\/uploads\/2023\/03\/python-export-sql-server-table-to-csv-file.jpg 720w, https:\/\/expert-only.com\/wp-content\/uploads\/2023\/03\/python-export-sql-server-table-to-csv-file-300x267.jpg 300w\" sizes=\"auto, (max-width: 720px) 100vw, 720px\" \/><figcaption class=\"wp-element-caption\"><em>How to export a SQL Server table into a CSV file in Python? Export content.<\/em><\/figcaption><\/figure><\/div>\n\n\n<p>Of course , you also need to replace and set these variables with your own values: <\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Driver<\/li>\n\n\n\n<li>Server<\/li>\n\n\n\n<li>Database<\/li>\n<\/ul>\n\n\n\n<p>And if you are not using a Windows account that has access to the source database to execute the Python script: <\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>User name<\/li>\n\n\n\n<li>Password<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">About exporting data from SQL Server to CSV files using Python<\/h3>\n\n\n\n<p>In conclusion, after following all the 4 steps outlined in this Python tutorial, you should now be able export a SQL Server tables into CSV files using Python programming. Remember to first connect to your SQL Server database using pyodbc, then retrieve the data from the table, and finally write the data to a CSV file using the csv module. Exporting data from a SQL Server database to a CSV file can be a useful skill for many data analysis and reporting tasks, especially for repetitive tasks with large amount of data.<\/p>\n\n\n\n<p>By automating this process with Python, you can save you hours of effort compared to doing it manually in Excel or other software. Indeed, Python provides a powerful set of tools for working with databases and data files.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">More Python tutorials about data and file manipulations<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/expert-only.com\/en\/python\/export-sql-server-table-to-excel-python\/\">Export SQL Server table to Excel in Python<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/expert-only.com\/en\/python\/work-with-sql-server-using-python\/\">Work with SQL Server using Python and pyodbc<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/expert-only.com\/en\/python\/text-files-in-python\/\">Manage text and csv files in Python using read and write methods<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/expert-only.com\/en\/python\/zip-and-unzip-files-in-python\/\">How to zip and unzip files in Python ?<\/a><\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-embed aligncenter is-type-wp-embed is-provider-sql-and-it-tutorials wp-block-embed-sql-and-it-tutorials\"><div class=\"wp-block-embed__wrapper\">\n<blockquote class=\"wp-embedded-content\" data-secret=\"l25m8bvaZr\"><a href=\"https:\/\/expert-only.com\/en\/python\/export-sql-server-table-to-excel-python\/\">Export SQL Server table to Excel in Python<\/a><\/blockquote><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"&#8220;Export SQL Server table to Excel in Python&#8221; &#8212; SQL and IT Tutorials\" src=\"https:\/\/expert-only.com\/en\/python\/export-sql-server-table-to-excel-python\/embed\/#?secret=bKQL55VSSA#?secret=l25m8bvaZr\" data-secret=\"l25m8bvaZr\" width=\"500\" height=\"282\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe>\n<\/div><\/figure>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>Scripts examples using Python code to export SQL Server data from a table to a CSV file. In this tutorial, we learn how to export data from a SQL Server table into a CSV file using only Python scripts <a class=\"mh-excerpt-more\" href=\"https:\/\/expert-only.com\/en\/python\/how-to-export-a-sql-server-table-into-a-csv-file-in-python\/\" title=\"How To Export A SQL Server Table Into A CSV File In Python?\">&#8230;<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":10864,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[657],"tags":[],"class_list":{"0":"post-22957","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-python"},"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v21.7 (Yoast SEO v26.2) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How To Export A SQL Server Table Into A CSV File In Python?<\/title>\n<meta name=\"description\" content=\"Tutorial with code examples on how to export a SQL Server table into a CSV file in Python, using the pyodbc and csv modules.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/expert-only.com\/en\/python\/how-to-export-a-sql-server-table-into-a-csv-file-in-python\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How To Export A SQL Server Table Into A CSV File In Python?\" \/>\n<meta property=\"og:description\" content=\"Tutorial with code examples on how to export a SQL Server table into a CSV file in Python, using the pyodbc and csv modules.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/expert-only.com\/en\/python\/how-to-export-a-sql-server-table-into-a-csv-file-in-python\/\" \/>\n<meta property=\"og:site_name\" content=\"SQL and IT Tutorials\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/ExpertOnlyCom\/\" \/>\n<meta property=\"article:published_time\" content=\"2024-01-08T05:36:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-02-23T14:50:30+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/09\/texture-design-7E25D084595_1920x1080.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1920\" \/>\n\t<meta property=\"og:image:height\" content=\"1080\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Expert-Only\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@expert_only\" \/>\n<meta name=\"twitter:site\" content=\"@expert_only\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Expert-Only\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/expert-only.com\/en\/python\/how-to-export-a-sql-server-table-into-a-csv-file-in-python\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/expert-only.com\/en\/python\/how-to-export-a-sql-server-table-into-a-csv-file-in-python\/\"},\"author\":{\"name\":\"Expert-Only\",\"@id\":\"https:\/\/expert-only.com\/en\/#\/schema\/person\/406a9576b52944f018739a42046873ef\"},\"headline\":\"How To Export A SQL Server Table Into A CSV File In Python?\",\"datePublished\":\"2024-01-08T05:36:00+00:00\",\"dateModified\":\"2024-02-23T14:50:30+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/expert-only.com\/en\/python\/how-to-export-a-sql-server-table-into-a-csv-file-in-python\/\"},\"wordCount\":940,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/expert-only.com\/en\/#organization\"},\"image\":{\"@id\":\"https:\/\/expert-only.com\/en\/python\/how-to-export-a-sql-server-table-into-a-csv-file-in-python\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/09\/texture-design-7E25D084595_1920x1080.jpg\",\"articleSection\":[\"Python tutorials\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/expert-only.com\/en\/python\/how-to-export-a-sql-server-table-into-a-csv-file-in-python\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/expert-only.com\/en\/python\/how-to-export-a-sql-server-table-into-a-csv-file-in-python\/\",\"url\":\"https:\/\/expert-only.com\/en\/python\/how-to-export-a-sql-server-table-into-a-csv-file-in-python\/\",\"name\":\"How To Export A SQL Server Table Into A CSV File In Python?\",\"isPartOf\":{\"@id\":\"https:\/\/expert-only.com\/en\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/expert-only.com\/en\/python\/how-to-export-a-sql-server-table-into-a-csv-file-in-python\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/expert-only.com\/en\/python\/how-to-export-a-sql-server-table-into-a-csv-file-in-python\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/09\/texture-design-7E25D084595_1920x1080.jpg\",\"datePublished\":\"2024-01-08T05:36:00+00:00\",\"dateModified\":\"2024-02-23T14:50:30+00:00\",\"description\":\"Tutorial with code examples on how to export a SQL Server table into a CSV file in Python, using the pyodbc and csv modules.\",\"breadcrumb\":{\"@id\":\"https:\/\/expert-only.com\/en\/python\/how-to-export-a-sql-server-table-into-a-csv-file-in-python\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/expert-only.com\/en\/python\/how-to-export-a-sql-server-table-into-a-csv-file-in-python\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/expert-only.com\/en\/python\/how-to-export-a-sql-server-table-into-a-csv-file-in-python\/#primaryimage\",\"url\":\"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/09\/texture-design-7E25D084595_1920x1080.jpg\",\"contentUrl\":\"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/09\/texture-design-7E25D084595_1920x1080.jpg\",\"width\":1920,\"height\":1080},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/expert-only.com\/en\/python\/how-to-export-a-sql-server-table-into-a-csv-file-in-python\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"en\",\"item\":\"https:\/\/expert-only.com\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How To Export A SQL Server Table Into A CSV File In Python?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/expert-only.com\/en\/#website\",\"url\":\"https:\/\/expert-only.com\/en\/\",\"name\":\"SQL and IT Tutorials\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/expert-only.com\/en\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/expert-only.com\/en\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/expert-only.com\/en\/#organization\",\"name\":\"Expert-Only\",\"url\":\"https:\/\/expert-only.com\/en\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/expert-only.com\/en\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/expert-only.com\/wp-content\/uploads\/2017\/09\/cropped-logo_Expert-Only.jpg\",\"contentUrl\":\"https:\/\/expert-only.com\/wp-content\/uploads\/2017\/09\/cropped-logo_Expert-Only.jpg\",\"width\":381,\"height\":174,\"caption\":\"Expert-Only\"},\"image\":{\"@id\":\"https:\/\/expert-only.com\/en\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/ExpertOnlyCom\/\",\"https:\/\/x.com\/expert_only\",\"https:\/\/www.youtube.com\/channel\/UCMS5sR_FwAetB0FmciNvUaA\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/expert-only.com\/en\/#\/schema\/person\/406a9576b52944f018739a42046873ef\",\"name\":\"Expert-Only\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/expert-only.com\/en\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/084b15660763ff5b13bb60b2f52f97bb?s=96&d=identicon&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/084b15660763ff5b13bb60b2f52f97bb?s=96&d=identicon&r=g\",\"caption\":\"Expert-Only\"}}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"How To Export A SQL Server Table Into A CSV File In Python?","description":"Tutorial with code examples on how to export a SQL Server table into a CSV file in Python, using the pyodbc and csv modules.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/expert-only.com\/en\/python\/how-to-export-a-sql-server-table-into-a-csv-file-in-python\/","og_locale":"en_US","og_type":"article","og_title":"How To Export A SQL Server Table Into A CSV File In Python?","og_description":"Tutorial with code examples on how to export a SQL Server table into a CSV file in Python, using the pyodbc and csv modules.","og_url":"https:\/\/expert-only.com\/en\/python\/how-to-export-a-sql-server-table-into-a-csv-file-in-python\/","og_site_name":"SQL and IT Tutorials","article_publisher":"https:\/\/www.facebook.com\/ExpertOnlyCom\/","article_published_time":"2024-01-08T05:36:00+00:00","article_modified_time":"2024-02-23T14:50:30+00:00","og_image":[{"width":1920,"height":1080,"url":"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/09\/texture-design-7E25D084595_1920x1080.jpg","type":"image\/jpeg"}],"author":"Expert-Only","twitter_card":"summary_large_image","twitter_creator":"@expert_only","twitter_site":"@expert_only","twitter_misc":{"Written by":"Expert-Only","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/expert-only.com\/en\/python\/how-to-export-a-sql-server-table-into-a-csv-file-in-python\/#article","isPartOf":{"@id":"https:\/\/expert-only.com\/en\/python\/how-to-export-a-sql-server-table-into-a-csv-file-in-python\/"},"author":{"name":"Expert-Only","@id":"https:\/\/expert-only.com\/en\/#\/schema\/person\/406a9576b52944f018739a42046873ef"},"headline":"How To Export A SQL Server Table Into A CSV File In Python?","datePublished":"2024-01-08T05:36:00+00:00","dateModified":"2024-02-23T14:50:30+00:00","mainEntityOfPage":{"@id":"https:\/\/expert-only.com\/en\/python\/how-to-export-a-sql-server-table-into-a-csv-file-in-python\/"},"wordCount":940,"commentCount":0,"publisher":{"@id":"https:\/\/expert-only.com\/en\/#organization"},"image":{"@id":"https:\/\/expert-only.com\/en\/python\/how-to-export-a-sql-server-table-into-a-csv-file-in-python\/#primaryimage"},"thumbnailUrl":"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/09\/texture-design-7E25D084595_1920x1080.jpg","articleSection":["Python tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/expert-only.com\/en\/python\/how-to-export-a-sql-server-table-into-a-csv-file-in-python\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/expert-only.com\/en\/python\/how-to-export-a-sql-server-table-into-a-csv-file-in-python\/","url":"https:\/\/expert-only.com\/en\/python\/how-to-export-a-sql-server-table-into-a-csv-file-in-python\/","name":"How To Export A SQL Server Table Into A CSV File In Python?","isPartOf":{"@id":"https:\/\/expert-only.com\/en\/#website"},"primaryImageOfPage":{"@id":"https:\/\/expert-only.com\/en\/python\/how-to-export-a-sql-server-table-into-a-csv-file-in-python\/#primaryimage"},"image":{"@id":"https:\/\/expert-only.com\/en\/python\/how-to-export-a-sql-server-table-into-a-csv-file-in-python\/#primaryimage"},"thumbnailUrl":"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/09\/texture-design-7E25D084595_1920x1080.jpg","datePublished":"2024-01-08T05:36:00+00:00","dateModified":"2024-02-23T14:50:30+00:00","description":"Tutorial with code examples on how to export a SQL Server table into a CSV file in Python, using the pyodbc and csv modules.","breadcrumb":{"@id":"https:\/\/expert-only.com\/en\/python\/how-to-export-a-sql-server-table-into-a-csv-file-in-python\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/expert-only.com\/en\/python\/how-to-export-a-sql-server-table-into-a-csv-file-in-python\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/expert-only.com\/en\/python\/how-to-export-a-sql-server-table-into-a-csv-file-in-python\/#primaryimage","url":"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/09\/texture-design-7E25D084595_1920x1080.jpg","contentUrl":"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/09\/texture-design-7E25D084595_1920x1080.jpg","width":1920,"height":1080},{"@type":"BreadcrumbList","@id":"https:\/\/expert-only.com\/en\/python\/how-to-export-a-sql-server-table-into-a-csv-file-in-python\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"en","item":"https:\/\/expert-only.com\/en\/"},{"@type":"ListItem","position":2,"name":"How To Export A SQL Server Table Into A CSV File In Python?"}]},{"@type":"WebSite","@id":"https:\/\/expert-only.com\/en\/#website","url":"https:\/\/expert-only.com\/en\/","name":"SQL and IT Tutorials","description":"","publisher":{"@id":"https:\/\/expert-only.com\/en\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/expert-only.com\/en\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/expert-only.com\/en\/#organization","name":"Expert-Only","url":"https:\/\/expert-only.com\/en\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/expert-only.com\/en\/#\/schema\/logo\/image\/","url":"https:\/\/expert-only.com\/wp-content\/uploads\/2017\/09\/cropped-logo_Expert-Only.jpg","contentUrl":"https:\/\/expert-only.com\/wp-content\/uploads\/2017\/09\/cropped-logo_Expert-Only.jpg","width":381,"height":174,"caption":"Expert-Only"},"image":{"@id":"https:\/\/expert-only.com\/en\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/ExpertOnlyCom\/","https:\/\/x.com\/expert_only","https:\/\/www.youtube.com\/channel\/UCMS5sR_FwAetB0FmciNvUaA"]},{"@type":"Person","@id":"https:\/\/expert-only.com\/en\/#\/schema\/person\/406a9576b52944f018739a42046873ef","name":"Expert-Only","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/expert-only.com\/en\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/084b15660763ff5b13bb60b2f52f97bb?s=96&d=identicon&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/084b15660763ff5b13bb60b2f52f97bb?s=96&d=identicon&r=g","caption":"Expert-Only"}}]}},"_links":{"self":[{"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/posts\/22957","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/comments?post=22957"}],"version-history":[{"count":6,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/posts\/22957\/revisions"}],"predecessor-version":[{"id":30017,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/posts\/22957\/revisions\/30017"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/media\/10864"}],"wp:attachment":[{"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/media?parent=22957"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/categories?post=22957"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/tags?post=22957"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}