如何使用Java在一个非常大的文件中交换行和列。

huangapple 未分类评论60阅读模式
标题翻译

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?

huangapple
  • 本文由 发表于 2020年1月30日 19:24:18
  • 转载请务必保留本文链接:https://java.coder-hub.com/59984921.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定