Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python reading a tab separated file using delimiter

Tags:

python

I am using the following to read a tab separated file .There are three columns in the file but the first column is being ignored when i print the column header only.how can i include the first column too

f = open("/tmp/data.txt")
for l in f.readlines():
  print l.strip().split("\t")
  break
  f.close()

Output: ['session_id\t', '\tevent_id_concat']

The first column name is id where it s not printed in the above array

EDIT

print l yields the following

EDIT 1:

   'id\tsession_id\tevent_id_concat\r\n'

   Output: ['id\t', '\tevent_id_concat'] 
like image 931
Rajeev Avatar asked Jan 09 '13 06:01

Rajeev


People also ask

How do you split a tab-delimited in Python?

Use the str. split() method to split a string by tabs, e.g. my_list = my_str. split('\t') .

What is a tab-delimited file in Python?

A tab-delimited file is a well-known and widely used text format for data exchange. By using a structure similar to that of a spreadsheet, it also allows users to present information in a way that is easy to understand and share across applications - including relational database management systems.

Is tab a delimiter?

A CSV (Comma Separated Values) or Tab-delimited Text (or Tab Separated Values) file is a text file in which one can identify rows and columns. Rows are represented by the lines in the file and the columns are created by separating the values on each line by a specific character, like a comma or a tab.


1 Answers

I would also suggest to use the csv module. It is easy to use and fits best if you want to read in table like structures stored in a CSV like format (tab/space/something else delimited).

The module documentation gives good examples where the simplest usage is stated to be:

import csv
with open('/tmp/data.txt', 'r') as f:
    reader = csv.reader(f)
    for row in reader:
        print row

Every row is a list which is very usefull if you want to do index based manipulations.

If you want to change the delimiter there is a keyword for this but I am often fine with the predefined dialects which can also be defined via a keyword.

import csv
with open('/tmp/data.txt', 'r') as f:
    reader = csv.reader(f, dialect='excel', delimiter='\t')
    for row in reader:
        print row

I am not sure if this will fix your problems but the use of elaborated modules will ensure you that something is wrong with your file and not your code if the error will remain.

like image 93
wagnerpeer Avatar answered Sep 30 '22 12:09

wagnerpeer