- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在使用 NumPy
的 genfromtext
从 CSV 文件中获取列。
每一列都需要拆分并分配给单独的 SQLAlchemy
SystemRecord
与其他一些列和属性结合并添加到数据库中。
迭代列 f1
到 f9
并将它们添加到 session 对象的最佳做法是什么?
到目前为止,我已经使用了以下代码,但我不想为每个 f
列做同样的事情:
t = np.genfromtxt(FILE_NAME,dtype=[(np.str_, 20),(np.str_, 20),(np.str_, 20),(np.str_, 20),(np.str_, 20),(np.str_, 20),(np.str_, 20),(np.str_, 20), (np.str_, 20), (np.str_, 20),(np.str_, 20)]\
,delimiter=',',filling_values="None", skiprows=0,usecols=(0,1,2,3,4,5,6,7,8,9,10))
for r in enumerate(t):
_acol = r['f1'].split('-')
_bcol = r['f2'].split('-')
....
arec = t_SystemRecords(first=_acol[0], second=_acol[1], third=_acol[2], ... )
db.session.add(arec)
db.session.commit()
最佳答案
查看t.dtype
。或者 r.dtype
。
制作一个示例结构化数组(这是 genfromtxt 返回的内容):
t = np.ones((5,), dtype='i4,i4,f8,S3')
看起来像:
array([(1, 1, 1.0, b'1'), (1, 1, 1.0, b'1'), (1, 1, 1.0, b'1'),
(1, 1, 1.0, b'1'), (1, 1, 1.0, b'1')],
dtype=[('f0', '<i4'), ('f1', '<i4'), ('f2', '<f8'), ('f3', 'S3')])
dtype
和 dtype.names
是:
In [135]: t.dtype
Out[135]: dtype([('f0', '<i4'), ('f1', '<i4'), ('f2', '<f8'), ('f3', 'S3')])
In [138]: t.dtype.names
Out[138]: ('f0', 'f1', 'f2', 'f3')
遍历名称以查看各个列:
In [139]: for n in t.dtype.names:
.....: print(t[n])
.....:
[1 1 1 1 1]
[1 1 1 1 1]
[ 1. 1. 1. 1. 1.]
[b'1' b'1' b'1' b'1' b'1']
或者在您的情况下,遍历“行”,然后遍历名称:
In [140]: for i,r in enumerate(t):
.....: print(r)
.....: for n in r.dtype.names:
.....: print(r[n])
.....:
(1, 1, 1.0, b'1')
1
1
1.0
b'1'
(1, 1, 1.0, b'1')
...
对于r
,它是0d(检查r.shape
),你可以通过数字选择项目或迭代
r[1] # == r[r.dtype.names[1]]
for i in r: print(r)
对于 1d 的 t
这不起作用; t[1]
引用了一个项目。
一维结构化数组的行为有点像二维数组,但又不完全是。通常所说的 row
和 column
必须替换为 row
(或 item)和 field
。
做一个可能更接近你的情况的t
In [175]: txt=[b'one-1, two-23, three-12',b'four-ab, five-ss, six-ss']
In [176]: t=np.genfromtxt(txt,dtype=[(np.str_,20),(np.str_,20),(np.str_,20)])
In [177]: t
Out[177]:
array([('one-1,', 'two-23,', 'three-12'),
('four-ab,', 'five-ss,', 'six-ss')],
dtype=[('f0', '<U20'), ('f1', '<U20'), ('f2', '<U20')])
np.char
具有可应用于数组的字符串函数:
In [178]: np.char.split(t['f0'],'-')
Out[178]: array([['one', '1,'], ['four', 'ab,']], dtype=object)
它不适用于结构化数组,但适用于各个字段。该输出可以作为列表的列表进行索引(它不是 2d)。
关于python - Numpy genfromtxt 遍历列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32256037/
我正在尝试读取以字符串形式存储在数据文件中的文件名。那里没问题。如果我将它传递给 genfromtxt,我会收到错误“IOError:Z:\Python\Rb input.txt not found”
我对 numpy.genfromtxt 函数有一个非常基本的问题。我正在使用 Enthought Canopy 包:我应该在哪里保存我想使用的 file.txt,或者我应该如何告诉 Python 在哪
我想使用 genfromtxt 读取一个 csv 文件。我有六列是 float ,一列是字符串。 如何设置数据类型,以便将 float 列作为 float 读入,将 string 列作为字符串读入?我
我有以下格式的数据 csv 文件: 130, 706, 249, 627, 428, 767, 430, 63, 884, 593 964, 340, 848, 607, 142, 517, 294
我正在尝试使用 scikit-learn 进行一些分类。但是,我在开始工作和导入数据时遇到了麻烦。 我正在处理这些数据:Adult Data Set . 我是 numpy 和 scikit-learn
我是 Python 的新手,目前在处理我的输入文件读取时遇到问题。基本上我希望我的代码采用一个输入文件,其中相关信息包含在 4 行的 block 中。对于我的特定目的,我只关心每个 block 的第
我有下表: 2M00251602+5422547 7.180 9.000 2.200 #2M00255540+5749320
我有一个 50,000x5,000 矩阵(浮点)文件。当使用 x = np.genfromtxt(readFrom, dtype=float) 将文件加载到内存中时,我收到以下错误消息: File "
我想知道当从具有多列的给定 (csv) 文件加载数据时如何替换特定值,结合字符串和数值。 在下面的示例中,假设您有多个地理位置,具有已知的纬度和经度以及一组特定的属性 (P1-P5) 和一个类(仅包含
在只加载一列数据的情况下,有没有办法强制 genfromtxt 输出形状为:(xx, 1) 的数据?通常的形状是(xx,)。我的示例中的 xx 可以是任何整数。 更新:这是代码示例: import n
我有一个 csv 文件,其中包含: 0812,EP2463,R,FEIGE 0812,EP2466,R,FEIGE 0816,EP2462,R,FEIGE 0816,EP2460,R,FEIGE 我需
我的包 numpy.genfromtxt 有一个奇怪的问题.我用它来读取包含多个列(可用 here )的数据文件,但即使 unpack 设置为 True,这些文件也不会被解压。 这是一个MWE: im
在问这个问题之前我已经阅读了这两个问题( q1 和 q2 )但是我没有找到任何令人满意的答案 我需要从二维数组中提取两列,而不是使用 pandas 或 loadtxt,而是使用 genfromtxt
我正在使用 numpy.genfromtxt 导入 CSV 文件。 要导入的数据有一个列名的标题,其中一些列名包含 genfromtxt 认为无效的字符。具体来说,一些名称包含“#”和“”。输入数据无
我的文件是这样的: 1497484825;34425;-4,28,-14;-4,28,-14;-4,28,-14;-4,28,-14;-4,28,-14;-4,28,-14 1497484837;34
使用 Python,我使用 genfromtxt(来自 numpy)将文本文件读入数组: y = np.genfromtxt("1400list.txt", dtype=[('mystring','S
我正在使用以下代码从 CSV 文件中读取大量数据 data = np.genfromtxt(inputfile.name, delimiter=',', dtype=float, names=True
我以为 genfromtxt() 会跳过 skip_header+skiprows,但事实并非如此。如果 skiprows 处于事件状态,则忽略 skip_header。 它们有什么区别? 最佳答案
我正在使用 NumPy 的 genfromtext 从 CSV 文件中获取列。 每一列都需要拆分并分配给单独的 SQLAlchemy SystemRecord 与其他一些列和属性结合并添加到数据库中。
如何加载 csv。当至少一个单元格为空时,将文件放入一个跳过行的数组中?我的 csv 文件很大(超过 1000 行和 14 列): 1;4;3 ;1;3 ;;6 3;4;7 我想跳过第 2 行和第 3
我是一名优秀的程序员,十分优秀!