How to transform list format of python to 2D presentation format and will be "tab" between the column as a text film in linux?
0
0
Entering edit mode
6.7 years ago
Grace_G ▴ 20

I use Linux, in one path, there is a file named annotation_x.txt

less annotation_x.txt
transcript:ENST00000437963  gene:ENSG00000187634
transcript:ENST00000624431  gene:ENSG00000239906
transcript:ENST00000335137  gene:ENSG00000186092

other python script annotation_x_mod.py to read it in

# coding=UTF-8
annotation_x = [line.split('\t') for line in open('annotation_x.txt')]  

python annotation_x_mod.py
[['transcript:ENST00000437963', 'gene:ENSG00000187634\n'], ['transcript:ENST00000624431', 'gene:ENSG00000239906\n'], ['transcript:ENST00000335137','gene:ENSG00000186092\n']]

How to reverse the process after some modification?

Like this simple example,it will become as:

[['transcript:ENST00000437963', '1', 'ENSG00000187634\n'], ['transcript:ENST00000624431', '12', 'ENSG00000239906\n'], ['transcript:ENST00000335137', '23', 'ENSG00000186092\n']]

then, how to return to linux and just shows by rows and cols?

transcript:ENST00000437963  1   ENSG00000187634
transcript:ENST00000624431  12  ENSG00000239906     
transcript:ENST00000335137  23  ENSG00000186092
python linux • 1.1k views
ADD COMMENT
1
Entering edit mode
for line in annotation_x:
    print(*line, sep="\t") # python3 or with from __future__ import print_function
    print "\t".join(line) # python2

What you also should do in your code is:

annotation_x = [line.strip().split('\t') for line in open('annotation_x.txt')] # use .strip() to get rid off the line endings
ADD REPLY
0
Entering edit mode

Exactly right! Many thanks!

ADD REPLY
1
Entering edit mode

Where did you get these 1, 12, 23 numbers from?

ADD REPLY
0
Entering edit mode

Your consideration is very reasonable. But here I just want to shows maybe will add one col or n cols. Actually for my data process I guess I will add some cols so gave example like 1, 12, 23.

ADD REPLY

Login before adding your answer.

Traffic: 2398 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6