Convert txt to csv python

Convert tab-delimited txt file into a csv file using Python

So I want to convert a simple tab delimited text file into a csv file. If I convert the txt file into a string using string.split(‘\n’) I get a list with each list item as a string with ‘\t’ between each column. I was thinking I could just replace the ‘\t’ with a comma but it won’t treat the string within the list like string and allow me to use string.replace. Here is start of my code that still needs a way to parse the tab «\t».

import csv import sys txt_file = r"mytxt.txt" csv_file = r"mycsv.csv" in_txt = open(txt_file, "r") out_csv = csv.writer(open(csv_file, 'wb')) file_string = in_txt.read() file_list = file_string.split('\n') for row in ec_file_list: out_csv.writerow(row) 

3 Answers 3

csv supports tab delimited files. Supply the delimiter argument to reader :

import csv txt_file = r"mytxt.txt" csv_file = r"mycsv.csv" # use 'with' if the program isn't going to immediately terminate # so you don't leave files open # the 'b' is necessary on Windows # it prevents \x1a, Ctrl-z, from ending the stream prematurely # and also stops Python converting to / from different line terminators # On other platforms, it has no effect in_txt = csv.reader(open(txt_file, "rb"), delimiter = '\t') out_csv = csv.writer(open(csv_file, 'wb')) out_csv.writerows(in_txt) 

-1 You are presuming that the OP is on Python 2.x; in that case the input file should be opened with ‘rb’ mode. Also not ensuring that at least the output file is closed, preferably both files.

Читайте также:  Пакеты классы объекты java

@JohnMachin I didn’t presume anything. I changed as little as possible to show how to convert a file. with isn’t necessary if the program is going to terminate immediately — the file will be closed. I added a comment to indicate care should be taken if it is a long running program.

It’s nothing to do preserving line endings. Read the docs for csv.reader for both Python 2.7 and 3.2. See also stackoverflow.com/questions/5180555/python-2-and-3-csv-reader

«The only difference between the two modes is how newlines are handled on Windows.»: Wrong; see my answer. What is «the encoding of the file is ASCII-compatible» supposed to mean? In any case, the sample data file in my answer is encoded in ASCII.

Why you should always use ‘rb’ mode when reading files with the csv module:

Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. 

What’s in the sample file: any old rubbish, including control characters obtained by extracting blobs or whatever from a database, or injudicious use of the CHAR function in Excel formulas, or .

>>> open('demo.txt', 'rb').read() 'h1\t"h2a\nh2b"\th3\r\nx1\t"x2a\r\nx2b"\tx3\r\ny1\ty2a\x1ay2b\ty3\r\n' 

Python follows CP/M, MS-DOS, and Windows when it reads files in text mode: \r\n is recognised as the line separator and is served up as \n , and \x1a aka Ctrl-Z is recognised as an END-OF-FILE marker.

>>> open('demo.txt', 'r').read() 'h1\t"h2a\nh2b"\th3\nx1\t"x2a\nx2b"\tx3\ny1\ty2a' # WHOOPS 

csv with a file opened with ‘rb’ works as expected:

>>> import csv >>> list(csv.reader(open('demo.txt', 'rb'), delimiter='\t')) [['h1', 'h2a\nh2b', 'h3'], ['x1', 'x2a\r\nx2b', 'x3'], ['y1', 'y2a\x1ay2b', 'y3']] 
>>> list(csv.reader(open('demo.txt', 'r'), delimiter='\t')) [['h1', 'h2a\nh2b', 'h3'], ['x1', 'x2a\nx2b', 'x3'], ['y1', 'y2a']] >>> 

Источник

How to export a txt file into csv with python?

How can i put these values into a csv with ignoring the first row (Monday, Jul 24) and insert the actual date in every rows?. I need this format:

A1 Monday, Jul 24 B1:8:45 apple 1 B2:school B3:ball B4:dog B5:ACTUALLY DATE C1:8:50 poirot 2 C2:rouge C3:chien C4:chat C5:ACTUALLY DATE etcetc.. 
import urllib2 import unicodecsv as csv import os import sys import io import time import datetime import pandas as pd from bs4 import BeautifulSoup import sys import re #def to_2d(l,n): # return [l[i:i+n] for i in range(0, len(l), n)] with open('air.txt', 'r') as f: x = f.read() print x req_text = x.split('Test')[1: -1] data = [] for text in req_text: text = text.split('\n', 1)[1] for line in text.strip().splitlines(): data.append([line]) #maindatatable = to_2d(data, 4) with open('output.csv', "wb") as f: output = csv.writer(f, delimiter=';',quotechar = '"', quoting=csv.QUOTE_NONNUMERIC, encoding='latin-1') output.writerows(data) f.close() 

I tried with the to_2d(l,n) but it isn’t work, any idea how can i reach this format with the actually date? [Python 2.7] Updated code, still not working:

import urllib2 import unicodecsv as csv import os import sys import io import time import datetime import pandas as pd from bs4 import BeautifulSoup import sys import re def to_2d(l,n): return [l[i:i+n] for i in range(0, len(l), n)] with open('air.txt', 'r') as f: x = f.read() print x req_text = x.split('Load')[1: -1] data = [] for text in req_text: text = text.split('\n', 1)[1] for line in text.strip().splitlines(): data.append([line]) #maindatatable = to_2d(data, 4) from string import ascii_uppercase as LETTERS with open('air.txt') as f, open('output.csv', 'wb') as g: actual_date = f.readline().strip() while True: first_line = f.readline().strip() if not first_line: break second_line = f.readline().strip() third_line = f.readline().strip() fourth_line = f.readline().strip() the_time, noun, number = first_line.split(' ') number = int(number) letter = LETTERS[number] new_line = '%s1:%s %s %s %s2:%s %s3:%s %s4:%s %s5:%s' % (letter, the_time, noun, number, letter, second_line, letter, third_line, letter, fourth_line, letter, actual_date) print (new_line) f.close() 

Источник

Convert Text File to CSV using Python (example included)

Data to Fish

You may use the following approach in order to convert a text file to a CSV file using Python:

import pandas as pd read_file = pd.read_csv (r'Path where the Text file is stored\File name.txt') read_file.to_csv (r'Path where the CSV will be saved\File name.csv', index=None)

In the next section, you’ll see the complete steps to convert your text file to CSV.

Steps to Convert a Text File to CSV using Python

Step 1: Install the Pandas package

If you haven’t already done so, install the Pandas package. You may use the following command to install the Pandas package under Windows:

Step 2: Capture the path where your text file is stored

Next, capture the path where the text file is stored on your computer.

Here is an example of a path where a text file (called ‘Product_List’) is stored:

Step 3: Specify the path where the new CSV file will be saved

Now, you’ll need to specify the path where the new CSV file will be saved. For example:

Step 4: Convert the text file to CSV using Python

Finally, you may use the template below in order to facilitate the conversion of your text file to CSV:

import pandas as pd read_file = pd.read_csv (r'Path where the Text file is stored\File name.txt') read_file.to_csv (r'Path where the CSV will be saved\File name.csv', index=None)
  • The path where the text file is stored is: C:\Users\Ron\Desktop\Test\Product_List.txt
    • Where the file name is Product_List and the file extension is txt
    • Where the new file name to be created is New_Products and the file extension is csv

    So this is the complete code to convert the text file to CSV for our example (note that you’ll need to modify the paths to reflect the location where the files are stored on your computer):

    import pandas as pd read_file = pd.read_csv (r'C:\Users\Ron\Desktop\Test\Product_List.txt') read_file.to_csv (r'C:\Users\Ron\Desktop\Test\New_Products.csv', index=None)

    Once you run the code in Python (adjusted to your paths), you’ll get the CSV file at your specified location.

    Источник

    Convert TXT To CSV Using Python Pandas

    In this article, you’ll learn how to convert a text (.txt) file into a CSV (comma-separated values) file using Python Pandas.

    Pandas is a fast, open-source library built on top of NumPy. We’ll use it to read a text file and convert it into a CSV file.

    Given below is the step-by-step guide to convert a text (.txt) file into a CSV file using Python Pandas.

    Steps To Convert Text (.txt) File To CSV Using Python Pandas

    Create The Original Text File With Rows And Columns

    First, create the original text file with the proper rows and columns structure.

    In this example, we’ll be using a text file containing the roll numbers, names, classes, and scores of a few students. The fields in the text file are separated by commas, which is the default delimiter. You can also use a different delimiter of your choice, as discussed later in this article.

    The text file, that we’re gonna convert to a CSV file in this example, is shown below.

    Python Pandas TXT to CSV: Original Text File

    Note: The first column is used for indexing by default when a text file is read.

    Convert The Text File Into A CSV File Using Python Panda’s Dataframe.to_csv() Method

    The Python code snippet required to convert a text file to CSV is given below:

    import pandas as pd textdatafile = pd.read_csv("path to the txt file") textdatafile.to_csv("new csv file name", index = None)

    After running the above code snippet a new csv file will be created in the same directory with the name mentioned inside the to_csv() function.

    Here’s how the outputted CSV file would look:

    Python Pandas TXT to CSV: Output CSV File

    Configurations

    Changing The Delimiter

    As mentioned earlier in this article, the read_csv() method, of the pandas library of Python, expects the fields in the text to be separated by commas (,) by default.

    However, we can include the delimiter parameter in the read_csv() to set our own delimiter.

    For example, in the text shown below, the fields are separated by the “~” character.

    Python Pandas TXT to CSV: Original Text File With Custom Delimiter

    In this case, we need to specify the delimiter in the read_csv() function as shown in the Python3 code snippet given below.

    import pandas as pd textdatafile = pd.read_csv("path to the txt file", delimiter = '~') textdatafile.to_csv("new csv file name", index = None) 

    The CSV output shown below.

    Python Pandas TXT to CSV: Output CSV File

    Specifying Own Headers

    The pandas library also allows us to specify our own headers for columns if they’re not already mentioned in the text file.

    Let’s take the example shown below, where there are no headers present for the columns.

    Python Pandas Convert Txt To Csv: Text File Without Column Headers

    In this case, we can insert the column headings using python pandas, while converting the file from TXT to CSV.

    The Python3 code snippet is given below.

    import pandas as pd textdatafile = pd.read_csv("path to the txt file", header = None) #adding column headings textdatafile.columns = ['Roll No.', 'Name', 'Class', 'Score'] textdatafile.to_csv("new csv file name", index = None) 

    When the code is executed, we get a new CSV file with all the fields and the column headings mentioned in the code. The CSV output is given below.

    Python Pandas TXT to CSV: Output CSV File

    I hope this article helped you learn how to convert a text file into a CSV file using the pandas library of Python.

    If you have any doubts or queries, feel free to comment down below. I will try my best to help you out.

    Anirban is a full-stack developer and an SEO expert. He has 6 years of experience in web development and software engineering. With a passion for writing, he has been blogging since 2017.

    Источник

Оцените статью