使用numpy.genfromtxt如何使用以下格式解析CSV字段? [英] Using numpy.genfromtxt how to parse a CSV field with the following format?

查看:870
本文介绍了使用numpy.genfromtxt如何使用以下格式解析CSV字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试解析一个CSV文件,其中有一个字段格式化为以下方式:

  [float ,float,float] 

问题是genfromtxt可以识别数组中的逗号作为CSV行。如何避免这样的事情?我现在做的是:

  genfromtxt(csv,skip_header = 2,dtype = None,delimiter =',')感谢

解决方案 div>

genfromtxt 需要像

这样的行

 float 
float,float,float

它不处理引号或括号。换句话说,真正的csv结构是简单的2d,没有内部分组或字段。



你可以给 genfromtxt $ csv code>阅读器可以处理报价,我怀疑你可以指示它治疗[]像报价。但是结果将是引用的文本的一个字符串。



genfromtxt 接受来自任何迭代的输入。所以你可以写一个函数,逐行读取文件,删除括号,根据需要添加分隔符,并将该行传递到 genfromtxt



对于一个开始,特别是对于小文件,加载整个东西作为行的列表( readlines ),转换为平分隔字符串。然后给 genfromtxt


I'm trying to parse a CSV file in which I have a field formatted in the following way:

[float,float,float]

the problem is that genfromtxt recognizes the commas inside my array as delimiter for the CSV row. How can I avoid such thing? What I'm doing now is:

genfromtxt(csv, skip_header=2, dtype=None, delimiter=',')

Thanks

解决方案

genfromtxt expects rows like

float, float, float
float, float, float

It does not handle quotes or brackets. In other words, the true csv structure is simple 2d, without internal groupings or 'fields'.

You can give genfromtxt a more complex dtype which does add structure.

The Python csv reader can handle quotes, and I suspect you can instruct it to treat [] like quotes. But the result will be one string for the 'quoted' text.

genfromtxt accepts input from any iterable. So you could write a little function that reads the file line by line, removes the brackets, adds delimiters as needed, and passes that line on to genfromtxt.

For a start, and especially for small files, load the whole thing as a list of lines (readlines), and massage those lines into flat delimited strings. Then give that to genfromtxt.

这篇关于使用numpy.genfromtxt如何使用以下格式解析CSV字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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