标题翻译
How to swap rows and columns in a very large file using java
问题
在Python中,我们可以在文件上使用迭代器(生成器函数),最后使用zip()函数,将具有相似索引的列映射并将这些列作为行写入新文件。这里有一个示例:
f = open('inp.tsv')
lines = (line.strip().split('\t') for line in list(f))
with open('out.tsv', 'a') as fo:
for line in zip(*lines):
print(*line, sep='\t', file=fo)
f.close()
假设文件具有以下内容:
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
结果:
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
那么在Java中如何执行类似的任务呢?
英文翻译
In Python, we can use iterator(generator function) on the file and finally use zip() which maps similarly indexed columns and write those columns as rows in a new file. Here is an example:
f=open('inp.tsv')
lines = (line.strip().split('\t') for line in list(f))
with open('out.tsv', 'a') as fo:
for line in zip(*lines):
print(*line, sep = '\t', file = fo)
f.close()
say the file has the following content:
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
Result:
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
How can we perform a similar task in java?
专注分享java语言的经验与见解,让所有开发者获益!
评论