Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

keep tabs after .split()

Tags:

python

I'm working on a small project and I encountered a problem.

I'm reading a file something like this (note, the original file has around 40k rows):

35   IMAGE:1679942   SAMD4   Sterile alpha motif domain   Hs.98259   ATI146610
36   IMAGE:1700154            AI049531
37   IMAGE:1865232            AI269361

As you can see, there are some cells, which are containing information, and some not. So I want that in every cell, where no information is stored, an N/A. How can I do this? .split() does make a list without these cells. Is there a solution, how can I keep all these taps in a list i.e. line = ["36", "IMAGE:1700154", "", "", "", "AI049531", ...]

like image 481
Jürgen Stürmer Avatar asked Aug 05 '26 17:08

Jürgen Stürmer


1 Answers

You can split at tabs (Edit Using data from comment):

data = """35\tIMAGE:1679942\tSAMD4\tSterile alpha motif domain\tHs.98259\tATI146610
36\tIMAGE:1700154\t\t\tAI049531"""

for line in data.split("\n"):
    print line.split("\t")

Result:

['35', 'IMAGE:1679942', 'SAMD4', 'Sterile alpha motif domain', 'Hs.98259', 'ATI146610']
['36', 'IMAGE:1700154', '', '', 'AI049531']