How to Read a Tsv File in Python

Files are used for storing information with the ability to read and write on them. The operations which can be performed on files in python are – read, write, open, close, rename and delete. In that location are two main types of files in python – binary file and text file. Binary files tin be of various types such every bit image files similar .png, .gif, .jpg or documents like .pdf, .xls, .doc, etc. The text file can be source code, web standards, tabular information, etc. In this commodity, we shall be looking into one such tabular data from the text file – .tsv file. Nosotros shall be seeing into how to read tsv file in python.

What is a TSV file?

The TSV file stands for tab-separated values file. It is a text file that stores information in a tabular form. The TSV file format is widely used for exchanging data between databases in the form of a database table or spreadsheet information. Here, each record is separated from the other past a tab character ( \t ). It acts as an alternating format to the .csv format. The difference between .tsv and .csv format is that the .csv format uses commas to split up columns in information whereas .tsv format uses tabs to carve up columns.

Reading TSV file in Python Using open Function

We tin can read the tsv file in python using the open up() office. We can read a given file with the help of the open() function. Subsequently reading, information technology returns a file object for the same. With open(), we can perform several file handling operations on the file such equally reading, writing, appending, and creating files.

Afterwards opening the file, nosotros shall make use of the reader() present in CSV to convert the file object into CSV.reader object. For using the reader, we shall exist starting time importing CSV .

Then, nosotros shall write the open() part. We shall be using a tsv file named 'product.tsv' , which consists of the sales count for three products over a span of 12 months. We will pass the tsv file as an argument to the open() role, and 'file' volition be the file'southward object.

Then we use csv.reader to convert the file object to csv.reader object. We pass the delimiter as '\t' to the csv.reader . The delimiter is used to betoken the grapheme which will exist separating each field.

Since this is a tsv file, we shall be passing the tab character as the delimiter. The variable 'tsv_file' will be the object for the tsv file. And so, we shall iterate the unabridged file and print each statement line by line.

with open("product.tsv") as file:     tsv_file = csv.reader(file, delimiter="\t")     for line in tsv_file:         print(line)          

The tsv file is printed line past line as the output:

['Month', 'Production A Sales', 'Product B Sales', 'Product C Sales'] ['Jan', '297', '119', '289'] ['Feb', '305', '437', '362'] ['March', '234', '247', '177'] ['April', '184', '193', '219'] ['May', '373', '316', '177'] ['June', '433', '169', '370'] ['July', '294', '403', '429'] ['August', '156', '445', '216'] ['September', '441', '252', '498'] ['October', '328', '472', '491'] ['November', '270', '251', '372'] ['December', '146', '159', '156']

The Entire Code is:

import csv with open("product.tsv") as file:     tsv_file = csv.reader(file, delimiter="\t")     for line in tsv_file:         print(line)          

Reading TSV file in Python Using Pandas

There is another way to read the tsv file which is using the pandas library. Pandas library in python is used for performing data analysis and data manipulation. It is a powerful library for manipulating numerical tables.

First, we shall be importing the pandas library.

Now, we shall be making use of the read_csv() part from the pandas library. Nosotros shall be passing the tsv file to the read_csv(). Along with the file, we shall be passing separator as '\t' for the tab character because, for tsv files, the tab character will split up each field.

tsv_data = pd.read_csv('production.tsv', sep='\t') tsv_data          

The output will be the tsv file:

read tsv file python

The Entire Code is:

import pandas as pd tsv_data = pd.read_csv('product.tsv', sep='\t') tsv_data          

Now, to read the first five rows from the production.tsv, nosotros shall make apply of head() function. This volition go the first n rows from the tsv file.

Past default, if you don't specify the number of rows, head() will impress 5 rows.

            Calendar month  Product A Sales  Product B Sales  Product C Sales 0   Jan              297              119              289 i  February              305              437              362 2     March              234              247              177 3     April              184              193              219 4       May              373              316              177          

To impress all the entries of a particular cavalcade, nosotros shall be using the post-obit lawmaking. Nosotros will print the unabridged 'Product A Sales' cavalcade.

print(tsv_data['Production A Sales'])          

The output will be:

0     297 1     305 2     234 3     184 4     373 5     433 6     294 7     156 8     441 ix     328 10    270 xi    146 Name: Product A Sales, dtype: int64

Writing Over a TSV File with Pandas

Now, we shall run into how to write over an already existing tsv file. We shall make use of the open() function merely this time we shall open the file in 'wt' mode. Using 'wt' mode, we can write the file as text. Instead of the csv.reader() , here we shall exist using csv.writer() . We shall pass the tsv file and the delimiter every bit '\t' to the writer() function.

After that, we shall use writerow() to write private rows to the file. Finally, we shall insert ii rows using the same function.

import csv  with open('product.tsv', 'wt') every bit file:     tsv_writer = csv.author(file, delimiter='\t')     tsv_writer.writerow(['Jan', 324, 122, 191])     tsv_writer.writerow(['Feb', 291, 322, 291])          

Now, permit u.s.a. endeavour to once more read the 'product.tsv' file. Once again, we shall utilise the same piece of lawmaking as used before for reading.

with open("product.tsv") as file:     tsv_file = csv.reader(file, delimiter="\t")     for line in tsv_file:         print(line)          

For the output, we can see that the file has been overwritten and information technology only contains two rows instead of the twelve rows which were present before.

['January', '324', '122', '191'] ['February', '291', '322', '291']

Writing TSV Without Pandas

To write over tsv files without using the pandas library, we shall use the post-obit code. Hither, we will append the contents of a file named 'total_sales' into another tsv file named 'product'. The 'total_sales' consists of sales for all the products for a twelvemonth, whereas the 'product' consists of sales for all in products individually.

with open("total_sales.tsv") equally file:   for line in file:     with open up('product.tsv', "a") as f:       f.write(line)          

Now, to read the file:

import csv with open("product.tsv") equally file:     tsv_file = csv.reader(file, delimiter="\t")     for line in tsv_file:         print(line)          

The output is:

['Month', 'Product A Sales', 'Product B Sales', 'Product C Sales'] ['Jan', '297', '119', '289'] ['February', '305', '437', '362'] ['March', '234', '247', '177'] ['Apr', '184', '193', '219'] ['May', '373', '316', '177'] ['June', '433', '169', '370'] ['July', '294', '403', '429'] ['August', '156', '445', '216'] ['September', '441', '252', '498'] ['Oct', '328', '472', '491'] ['November', '270', '251', '372'] ['December', '146', '159', '156Month', 'Full Sales'] ['Jan', '558'] ['February', '871'] ['March', '756'] ['Apr', '509'] ['May', '987'] ['June', '625'] ['July', '862'] ['August', '548'] ['September', '669'] ['Oct', '827'] ['November', '776'] ['Dec', '955']

As seen above, the 'product' file has been appended with the contents of the 'total_sales' file.

Reading TSV into dictionary with open()

We tin read a given tsv file and store its contents into a dictionary. To achieve that, nosotros shall be taking a tsv file containing two columns – calendar month and total sales. And so, with the assistance of the open() function, we shall store each calendar month equally the dictionary's key and the full sales amount for the month as the values.

Nosotros shall split the calendar month and sales using the tab grapheme. And so, we shall enumerate over the dictionary and impress its values.

sales_dictionary = {} with open up("total_sales.tsv") every bit f:   for line in f:     (calendar month, sales)=line.dissever('\t')     sales_dictionary[month]=sales  for i,calendar month in enumerate(sales_dictionary):   print(f'{calendar month} : {sales_dictionary[month]}')          

The output is:

Calendar month : Total Sales  January : 558  February : 871  March : 756  Apr : 509  May : 987  June : 625  July : 862  August : 548  September : 669  October : 827  November : 776  Dec : 955

Must, Read


That sums up everything most the tsv file. If you lot accept any questions, let united states know in the comments below.

Until next time, Go on Learning!

lawyeroveregic.blogspot.com

Source: https://www.pythonpool.com/read-tsv-file-python/

0 Response to "How to Read a Tsv File in Python"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel