读入数组时忽略字符串 [英] ignoring string while reading into an array

查看:67
本文介绍了读入数组时忽略字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将.csv文件加载到数组中. 但是,该文件看起来像这样.

I am trying to load a .csv file into an array. However, the file looks something like this.

"myfilename",0.034353453,-1.234556,-3,45671234
,1.43567896, -1.45322124, 9.543422
 .................................
 .................................

我正在尝试跳过前导字符串.到目前为止,我一直在取消第一行.

I am trying to skip the leading string. I've been doing away with the first row till now.

 a = np.genfromtxt(file,delimiter=',',skiprows=1)   

但是我想知道是否有一种方法可以在处理开始时忽略数组中的字符串而读入数组.

But I was wondering if there's a way to read into an array ignoring the string at the beginning in processing.

推荐答案

您能只使用loadtxt(..., usecols=(1,2,3), ...)来避免在文件开头跳过一行吗?

Can you just use loadtxt(..., usecols=(1,2,3), ...), which avoids skipping a line at the start of the file?

usecols参数仅告诉loadtxt要提取的列(是数字列)

The usecols argument just tells loadtxt which columns to extract (and are numeric)

# Put data into file (in shell, just me copying the sample)
cat >> /tmp/data.csv
"myfilename",0.034353453,-1.234556,-3,45671234
,1.43567896, -1.45322124, 9.543422

# In IPython
In [1]: import numpy as np

In [2]: a = np.loadtxt('/tmp/data.csv', usecols=(1,2,3), delimiter=',')

In [3]: a
Out[3]: 
array([[ 0.03435345, -1.234556  , -3.        ],
       [ 1.43567896, -1.45322124,  9.543422  ]])

这篇关于读入数组时忽略字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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