简体   繁体   中英

python reading a tab separated file using delimiter

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'] 

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.

It should work but it is better to use 'with':

with open('/tmp/data.txt') as f:
   for l in f:
       print l.strip().split("\t")

if it doesn't then probably your file doesn't have the required format.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM