使用python将文件加载到numpy数组中 [英] Loading a file into a numpy array with python

查看:327
本文介绍了使用python将文件加载到numpy数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我对Python非常绿色,并且试图通过复制我编写的一些matlab代码来学习。我有一个部分,在matlab中,我加载一个数据文件,制表符分隔。语法

  x = load(data.txt)

获取制表符分隔的数据,并将其放入一个标有x的矩阵的单元格中。



有没有办法在python中做这个,但是用逗号分隔的数据?

解决方案

有几种方法,选择最适合您应用程序的方法。



如果您使用 numpy ,可能最好使用numpy的 载入 loadtxt fromfile genfromtxt 功能,因为您的文件



但如果你不打算使用numpy(或任何其他大的库它有一些文件加载​​功能),它将是一个overkill使用它只是用于加载文件...考虑使用内置的python函数,或csv module from the standard library instead ...它会更灵活,更流畅。



这里是如何,使用 file.txt (每行的值用制表符分隔)的示例:

  1 2 3 4 
7 8 9 10 11 12
13 14 15



python内置



无需导入模块,非常容易,灵活,是大多数情况下的理想选择。

在二进制模式下加载文件以读取( rb 标志)(表中的值列表列表与选项卡)仅具有内置函数:

 >> file = open('file.txt','rb')
>>> table = [row.strip()。split('\t')for file in file]



csv



csv模块



请注意,CSV表示逗号分隔值,实际上没有标准,您可以选择任何分隔符。因此,CSV代表所有面向单元格或表格的文件。



以二进制模式加载文件以进行读取( rb flags)在csv 读者

  >> import csv 
>>>> file = open('file.txt','rb')
>>> data = csv.reader(file,delimiter ='\t')
>>> table = [row for data in data]



访问单元格



表与上面的两个例子类似,并且可以像 table [row] [col]

 >>> table 
[['1','2','3','4'],['7','8','9','10','11','12' ['13','14','15']]
>>>> table [0]
['1','2','3','4']
>>> table [1] [2]
9


So I'm very green with Python and am trying to learn by replicating some matlab code I've written. I have a part where, in matlab, I load a data file that's tab-delimited. The syntax

x = load(data.txt)

Takes the tab delimited data and put them into cells of a matrix labeled x.

Is there a way to do this in python, but with comma-delimited data?

解决方案

There are several methods, choose one that is most suitable for your application.

If you are working with numpy, it may be a good idea to use the numpy's load, loadtxt, fromfile or genfromtxt functions, because your file will be loaded into a suitable structure, after the preprocessing.

But if you are not about to work with numpy (or any other big library which has some file loading functionalities), it would be an overkill using it just for loading a file ... Consider using built-in python functions, or the csv module from the standard library instead ... It will be much more flexible, and way smoother.

Here is how, with examples using file.txt (values of each rows are separated with tabs):

1   2   3   4
7   8   9   10  11  12
13  14  15

python built-in

No module to import, pretty easy, flexible, a good option for most situations, imho.

Loading the file in binary mode for reading (rb flags) in a table (list of lists of values, separated in the file with tabs) with only built-in functions:

>>> file = open('file.txt', 'rb')
>>> table = [row.strip().split('\t') for row in file]

csv

The csv module from the standard library is pretty straightforward as well.

Note that altough CSV means Comma Separated Values, there is actually no standard and you can choose any delimiter you want. Therefore CSV stands for all cells-oriented or table-like files.

Loading the file in binary mode for reading (rb flags) in a table (list of lists of values, separated in the file with tabs) with the csv reader:

>>> import csv
>>> file = open('file.txt', 'rb')
>>> data = csv.reader(file, delimiter='\t')
>>> table = [row for row in data]

Accessing the cells

The table has been loaded similarly with the two previous examples, and the data of the table can be accessed like table[row][col]:

>>> table
[['1', '2', '3', '4'], ['7', '8', '9', '10', '11', '12'], ['13', '14', '15']]    
>>> table[0]
['1', '2', '3', '4']
>>> table[1][2]
9

这篇关于使用python将文件加载到numpy数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆