0

I am trying to split by "\t" but it is not printing all the elements in it

import sys
reload(sys)  
sys.setdefaultencoding('utf8')

s = ['A\t"Ravi"\t"Tirupur"\t"India"\t"641652"\t"arunachalamravi@gmail.com"\t"17379602"\t"+ 2"\t"Government Higher Secondary School', ' Tiruppur"\t\t"1989"\t"Maths',' Science"\t"No"\t"Encotec Energy 2 X 600 MW ITPCL"\t"Associate Vice President- Head Maintenance"\t"2015"\t"2016"\t"No"\t"27-Mar-2017"\t"9937297878"\t\t"2874875"\t"Submitted"\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t']
print s[0].split("\t")

Results

['A', '"Ravi"', '"Tirupur"', '"India"', '"641652"', '"arunachalamravi@gmail.com"', '"17379602"', '"+ 2"', '"Government Higher Secondary School']

But i want results upto this

2874875, Submitted

How to fix the code and where is the change?

DSM
  • 342,061
  • 65
  • 592
  • 494

3 Answers3

0

Easy, you have more than one item in your list so when you do s[0] you just get the first one, fix your list or fix your code like this:

joined_string = ''.join(s)
print joined_string.split("\t")

It should work

Seif
  • 1,058
  • 11
  • 19
0

With your data you should do something like this:

s[2].split("\t")[10:12]
GLR
  • 1,070
  • 1
  • 11
  • 29
0

You could use Python's chain() function to create a single list from the multiple elements:

from itertools import chain

s = ['A\t"Ravi"\t"Tirupur"\t"India"\t"641652"\t"arunachalamravi@gmail.com"\t"17379602"\t"+ 2"\t"Government Higher Secondary School', ' Tiruppur"\t\t"1989"\t"Maths',' Science"\t"No"\t"Encotec Energy 2 X 600 MW ITPCL"\t"Associate Vice President- Head Maintenance"\t"2015"\t"2016"\t"No"\t"27-Mar-2017"\t"9937297878"\t\t"2874875"\t"Submitted"\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t']

result = list(chain.from_iterable(x.rstrip('\t').split('\t') for x in s))
print result

This would give you all of the split entries, and remove the trailing tabs from the end:

['A', '"Ravi"', '"Tirupur"', '"India"', '"641652"', '"arunachalamravi@gmail.com"', '"17379602"', '"+ 2"', '"Government Higher Secondary School', ' Tiruppur"', '', '"1989"', '"Maths', ' Science"', '"No"', '"Encotec Energy 2 X 600 MW ITPCL"', '"Associate Vice President- Head Maintenance"', '"2015"', '"2016"', '"No"', '"27-Mar-2017"', '"9937297878"', '', '"2874875"', '"Submitted"']

If you also want to get rid of the quotes, then use this instead:

result = [v.strip('"') for v in chain.from_iterable(x.rstrip('\t').split('\t') for x in s)]    

Giving you:

['A', 'Ravi', 'Tirupur', 'India', '641652', 'arunachalamravi@gmail.com', '17379602', '+ 2', 'Government Higher Secondary School', ' Tiruppur', '', '1989', 'Maths', ' Science', 'No', 'Encotec Energy 2 X 600 MW ITPCL', 'Associate Vice President- Head Maintenance', '2015', '2016', 'No', '27-Mar-2017', '9937297878', '', '2874875', 'Submitted']
Martin Evans
  • 45,791
  • 17
  • 81
  • 97