我如何压缩()用制表符分隔的文件的多行以便转置文件的内容?

问题描述 投票:0回答:1

说文件具有以下内容:

Xkr4    0   0   0   0
Gm1992  0   0   0   0
Gm37381 0   0   0   0
Rp1 0   0   0   0
Rp1.1   0   0   0   0
Sox17   0   0   0   0
f=open(tsv_path, 'r')
transposed_iterator = zip(*csv.reader(open(tsv_path), delimiter = '\t'))
with open(output_tsv_path, 'w') as out:
    for row in transposed_iterator:
        out.write(delimiter.join(row) + '\n')

结果:

Xkr4    Gm1992  Gm37381 Rp1 Rp1.1   Sox17
0   0   0   0   0   0
0   0   0   0   0   0
0   0   0   0   0   0
0   0   0   0   0   0

上面的代码正是我想要的,但是问题是我使用的是非常老的jython,它不包含csv模块。没有CSV模块怎么办?

python csv zip jython readfile
1个回答
1
投票

您可以尝试:

f=open('text.txt')
lines = (line.strip().split() for line in list(f))

with open('otext.txt', 'a') as fo:
    for line in zip(*lines):
        print(*line, sep = '\t', file = fo)

f.close()
© www.soinside.com 2019 - 2024. All rights reserved.