{"id":2924,"date":"2022-08-17T06:36:49","date_gmt":"2022-08-17T01:36:49","guid":{"rendered":"https:\/\/www.edopedia.com\/blog\/?p=2924"},"modified":"2022-08-17T06:50:18","modified_gmt":"2022-08-17T01:50:18","slug":"convert-text-file-txt-to-csv-using-python3-pandas-script","status":"publish","type":"post","link":"https:\/\/www.edopedia.com\/blog\/convert-text-file-txt-to-csv-using-python3-pandas-script\/","title":{"rendered":"Convert Text File (TXT) to CSV Using Python 3 Pandas Script"},"content":{"rendered":"\n<p>This tutorial will teach you how to convert a <strong>Text (.txt) file to CSV<\/strong> using the <a href=\"https:\/\/pandas.pydata.org\/\">pandas library<\/a> in <strong>Python<\/strong>. I&#8217;ll give you the complete <strong>source code<\/strong> of this application so you could easily test it out on your PC.<\/p>\n\n\n\n<p>Let&#8217;s get started by installing the pandas library using the <code>pip<\/code> command.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;shell&quot;,&quot;mime&quot;:&quot;text\/x-sh&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Shell&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;shell&quot;}\">pip install pandas<\/pre><\/div>\n\n\n\n<p>Now create a new Python file for this project and name it as <code>code.py<\/code>. After that, copy the below-given code-snippet and paste it in <code>code.py<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">code.py<\/h2>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\"># importing pandas library\nimport pandas as pd\n\n# reading given csv from .txt file\n# and creating dataframe\ndataframe = pd.read_csv(&quot;file.txt&quot;, delimiter = ',')\n  \n# storing this dataframe in a csv file\ndataframe.to_csv('new_file.csv', index = None)<\/pre><\/div>\n\n\n\n<p>According to our code, we need to provide a <code>file.txt<\/code> that contains our data. By default, commas will be used as delimiters, but you can easily change them to whatever character you like.<\/p>\n\n\n\n<p>So, let&#8217;s create a <code>file.txt<\/code> in the root folder of your project and add some comma-separated values. For example, have a look at this file.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">file.txt<\/h2>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;null&quot;,&quot;mime&quot;:&quot;text\/plain&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Plain Text&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;text&quot;}\">ID, Name, Score\n1, Furqan, 100\n2, Ibrar, 95\n3, Ammar, 87\n4, Sheraz, 90\n5, Babur, 65<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Run Python Convert TXT to CSV Project<\/h2>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;shell&quot;,&quot;mime&quot;:&quot;text\/x-sh&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Shell&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;shell&quot;}\">python code.py<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">The Output of Python TXT to CSV<\/h2>\n\n\n\n<p>A file named <code>new_file.csv<\/code> will be generated automatically in your project&#8217;s root folder.<\/p>\n\n\n\n<p>Here&#8217;s a screenshot of that file:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"546\" src=\"https:\/\/www.edopedia.com\/blog\/wp-content\/uploads\/2022\/08\/Screenshot-2022-08-17-061036-1024x546.png\" alt=\"python convert txt to csv\" class=\"wp-image-2926\" title=\"python convert txt to csv\" srcset=\"https:\/\/www.edopedia.com\/blog\/wp-content\/uploads\/2022\/08\/Screenshot-2022-08-17-061036-1024x546.png 1024w, https:\/\/www.edopedia.com\/blog\/wp-content\/uploads\/2022\/08\/Screenshot-2022-08-17-061036-300x160.png 300w, https:\/\/www.edopedia.com\/blog\/wp-content\/uploads\/2022\/08\/Screenshot-2022-08-17-061036-768x409.png 768w, https:\/\/www.edopedia.com\/blog\/wp-content\/uploads\/2022\/08\/Screenshot-2022-08-17-061036.png 1366w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial will teach you how to convert a Text (.txt) file to CSV using the pandas library in Python. I&#8217;ll give you the complete source code of this application so you could easily test it out on your PC. Let&#8217;s get started by installing the pandas library using the pip command. Now create a &#8230; <a title=\"Convert Text File (TXT) to CSV Using Python 3 Pandas Script\" class=\"read-more\" href=\"https:\/\/www.edopedia.com\/blog\/convert-text-file-txt-to-csv-using-python3-pandas-script\/\" aria-label=\"Read more about Convert Text File (TXT) to CSV Using Python 3 Pandas Script\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":1762,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[112],"tags":[],"class_list":["post-2924","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Convert Text File (TXT) to CSV Using Python 3 Pandas Script<\/title>\n<meta name=\"description\" content=\"This tutorial will teach you how to convert a Text (.txt) file to CSV using the pandas library in Python. I&#039;ll give you the complete source code of this\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.edopedia.com\/blog\/convert-text-file-txt-to-csv-using-python3-pandas-script\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Convert Text File (TXT) to CSV Using Python 3 Pandas Script\" \/>\n<meta property=\"og:description\" content=\"This tutorial will teach you how to convert a Text (.txt) file to CSV using the pandas library in Python. I&#039;ll give you the complete source code of this\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.edopedia.com\/blog\/convert-text-file-txt-to-csv-using-python3-pandas-script\/\" \/>\n<meta property=\"og:site_name\" content=\"Edopedia\" \/>\n<meta property=\"article:author\" content=\"trulyfurqan\" \/>\n<meta property=\"article:published_time\" content=\"2022-08-17T01:36:49+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-08-17T01:50:18+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.edopedia.com\/blog\/wp-content\/uploads\/2022\/08\/Screenshot-2022-08-17-061036-1024x546.png\" \/>\n<meta name=\"author\" content=\"Furqan\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Furqan\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Convert Text File (TXT) to CSV Using Python 3 Pandas Script","description":"This tutorial will teach you how to convert a Text (.txt) file to CSV using the pandas library in Python. I'll give you the complete source code of this","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:\/\/www.edopedia.com\/blog\/convert-text-file-txt-to-csv-using-python3-pandas-script\/","og_locale":"en_US","og_type":"article","og_title":"Convert Text File (TXT) to CSV Using Python 3 Pandas Script","og_description":"This tutorial will teach you how to convert a Text (.txt) file to CSV using the pandas library in Python. I'll give you the complete source code of this","og_url":"https:\/\/www.edopedia.com\/blog\/convert-text-file-txt-to-csv-using-python3-pandas-script\/","og_site_name":"Edopedia","article_author":"trulyfurqan","article_published_time":"2022-08-17T01:36:49+00:00","article_modified_time":"2022-08-17T01:50:18+00:00","og_image":[{"url":"https:\/\/www.edopedia.com\/blog\/wp-content\/uploads\/2022\/08\/Screenshot-2022-08-17-061036-1024x546.png","type":"","width":"","height":""}],"author":"Furqan","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Furqan","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.edopedia.com\/blog\/convert-text-file-txt-to-csv-using-python3-pandas-script\/#article","isPartOf":{"@id":"https:\/\/www.edopedia.com\/blog\/convert-text-file-txt-to-csv-using-python3-pandas-script\/"},"author":{"name":"Furqan","@id":"https:\/\/www.edopedia.com\/blog\/#\/schema\/person\/3951cb19e3aa56df09e408c98aa02339"},"headline":"Convert Text File (TXT) to CSV Using Python 3 Pandas Script","datePublished":"2022-08-17T01:36:49+00:00","dateModified":"2022-08-17T01:50:18+00:00","mainEntityOfPage":{"@id":"https:\/\/www.edopedia.com\/blog\/convert-text-file-txt-to-csv-using-python3-pandas-script\/"},"wordCount":176,"commentCount":0,"publisher":{"@id":"https:\/\/www.edopedia.com\/blog\/#organization"},"image":{"@id":"https:\/\/www.edopedia.com\/blog\/convert-text-file-txt-to-csv-using-python3-pandas-script\/#primaryimage"},"thumbnailUrl":"https:\/\/www.edopedia.com\/blog\/wp-content\/uploads\/2022\/02\/default_featured_image.jpg","articleSection":["Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.edopedia.com\/blog\/convert-text-file-txt-to-csv-using-python3-pandas-script\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.edopedia.com\/blog\/convert-text-file-txt-to-csv-using-python3-pandas-script\/","url":"https:\/\/www.edopedia.com\/blog\/convert-text-file-txt-to-csv-using-python3-pandas-script\/","name":"Convert Text File (TXT) to CSV Using Python 3 Pandas Script","isPartOf":{"@id":"https:\/\/www.edopedia.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.edopedia.com\/blog\/convert-text-file-txt-to-csv-using-python3-pandas-script\/#primaryimage"},"image":{"@id":"https:\/\/www.edopedia.com\/blog\/convert-text-file-txt-to-csv-using-python3-pandas-script\/#primaryimage"},"thumbnailUrl":"https:\/\/www.edopedia.com\/blog\/wp-content\/uploads\/2022\/02\/default_featured_image.jpg","datePublished":"2022-08-17T01:36:49+00:00","dateModified":"2022-08-17T01:50:18+00:00","description":"This tutorial will teach you how to convert a Text (.txt) file to CSV using the pandas library in Python. I'll give you the complete source code of this","breadcrumb":{"@id":"https:\/\/www.edopedia.com\/blog\/convert-text-file-txt-to-csv-using-python3-pandas-script\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.edopedia.com\/blog\/convert-text-file-txt-to-csv-using-python3-pandas-script\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.edopedia.com\/blog\/convert-text-file-txt-to-csv-using-python3-pandas-script\/#primaryimage","url":"https:\/\/www.edopedia.com\/blog\/wp-content\/uploads\/2022\/02\/default_featured_image.jpg","contentUrl":"https:\/\/www.edopedia.com\/blog\/wp-content\/uploads\/2022\/02\/default_featured_image.jpg","width":880,"height":495,"caption":"Default Featured Image"},{"@type":"BreadcrumbList","@id":"https:\/\/www.edopedia.com\/blog\/convert-text-file-txt-to-csv-using-python3-pandas-script\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.edopedia.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Convert Text File (TXT) to CSV Using Python 3 Pandas Script"}]},{"@type":"WebSite","@id":"https:\/\/www.edopedia.com\/blog\/#website","url":"https:\/\/www.edopedia.com\/blog\/","name":"Edopedia","description":"Coding\/Programming Blog","publisher":{"@id":"https:\/\/www.edopedia.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.edopedia.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.edopedia.com\/blog\/#organization","name":"Edopedia","url":"https:\/\/www.edopedia.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.edopedia.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.edopedia.com\/blog\/wp-content\/uploads\/2017\/10\/edopedia_icon_text_10.jpg","contentUrl":"https:\/\/www.edopedia.com\/blog\/wp-content\/uploads\/2017\/10\/edopedia_icon_text_10.jpg","width":400,"height":100,"caption":"Edopedia"},"image":{"@id":"https:\/\/www.edopedia.com\/blog\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/www.edopedia.com\/blog\/#\/schema\/person\/3951cb19e3aa56df09e408c98aa02339","name":"Furqan","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/e5e68aef3ad8f0b83d56f4953c512c8e57bd2e6dc64daec33b5d0495d9058f51?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/e5e68aef3ad8f0b83d56f4953c512c8e57bd2e6dc64daec33b5d0495d9058f51?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/e5e68aef3ad8f0b83d56f4953c512c8e57bd2e6dc64daec33b5d0495d9058f51?s=96&d=mm&r=g","caption":"Furqan"},"description":"Well. I've been working for the past three years as a web designer and developer. I have successfully created websites for small to medium sized companies as part of my freelance career. During that time I've also completed my bachelor's in Information Technology.","sameAs":["http:\/\/www.edopedia.com\/blog\/","trulyfurqan"],"url":"https:\/\/www.edopedia.com\/blog\/author\/furqan\/"}]}},"_links":{"self":[{"href":"https:\/\/www.edopedia.com\/blog\/wp-json\/wp\/v2\/posts\/2924","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.edopedia.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.edopedia.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.edopedia.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.edopedia.com\/blog\/wp-json\/wp\/v2\/comments?post=2924"}],"version-history":[{"count":0,"href":"https:\/\/www.edopedia.com\/blog\/wp-json\/wp\/v2\/posts\/2924\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.edopedia.com\/blog\/wp-json\/wp\/v2\/media\/1762"}],"wp:attachment":[{"href":"https:\/\/www.edopedia.com\/blog\/wp-json\/wp\/v2\/media?parent=2924"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.edopedia.com\/blog\/wp-json\/wp\/v2\/categories?post=2924"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.edopedia.com\/blog\/wp-json\/wp\/v2\/tags?post=2924"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}