如何 zip() 文件的多行,其中制表符分隔以转置文件内容?



表示该文件包含以下内容:

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

上面的行完全符合我的需求,但问题是我使用的是不包含 csv 模块的非常旧的 jython。没有csv模块怎么办?

你可以试试这个:

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()

最新更新