在文件中转置数据网格的最佳方法

发布于 2024-08-08 06:45:13 字数 201 浏览 1 评论 0原文

我在二维网格上有大量值的数据文件。 它们的组织方式使得网格中的后续数据行是文件中的后续行。 每列由制表符分隔。 本质上,这是一个 CSV 文件,但带有选项卡而不是列。

我需要转置数据(第一行变成第一列)并将其输出到另一个文件。最好的方法是什么?任何语言都可以(我更喜欢使用 Perl 或 C/C++)。目前,我的 Perl 脚本只是将整个文件读入内存,但我的文件非常巨大。

I have large data files of values on a 2D grid.
They are organized such that subsequent rows of data in the grid are subsequent lines in the file.
Each column is separated by a tab character.
Essentially, this is a CSV file, but with tabs instead of columns.

I need the transpose the data (first row becomes first column) and output it to another file. What's the best way to do this? Any language is okay (I prefer to use Perl or C/C++). Currently, I have Perl script just read in the entire file into memory, but I have files which are simply gigantic.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

二智少女 2024-08-15 06:45:13

最简单的方法是对输入进行多次传递,在每次传递中提取列的子集。列数将取决于您想要使用多少内存以及输入文件中有多少行。

例如:

在第 1 遍中,您读取整个输入文件并仅处理前 10 列。如果输入有 100 万行,输出将是一个有 100 万列和 10 行的文件。在下一次传递中,您将再次读取输入,并处理第 11 列到第 20 列,将结果附加到原始输出文件中。等等....

The simplest way would be to make multiple passes through your input, extracting a subset of columns on each pass. The number of columns would be determined by how much memory you wanted to use and how many rows are in the input file.

For example:

On pass 1 you read the entire input file and process only the first, say, 10 columns. If the input had 1 million rows, the output would be a file with 1 million columns and 10 rows. On the next pass you would read the input again, and process columns 11 thru 20, appending the results to the original output file. And so on....

_畞蕅 2024-08-15 06:45:13

如果您安装了安装了 NumPy 的 Python,那么就像这样简单:

#!/usr/bin/env python

import numpy, csv

with open('/path/to/data.csv', 'rb') as file:
    csvdata = csv.reader()

data = numpy.array(csvdata)
transpose = data.T

... csv 模块是 Python 标准库的一部分。

If you have Python with NumPy installed, it's as easy as this:

#!/usr/bin/env python

import numpy, csv

with open('/path/to/data.csv', 'rb') as file:
    csvdata = csv.reader()

data = numpy.array(csvdata)
transpose = data.T

... the csv module is part of Python's standard library.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文