numpy genfromtxt strings int混合数据类型

时间:2021-02-20 16:26:25

I'm trying to read a csv file with the following content into a numpy array:

我正在尝试将具有以下内容的csv文件读入numpy数组:

1,85,104,2,"C"
2,71,82,2,"C#"
3,67,73,2,"D"
4,105,108,2,"D#"
5,103,100,2,"E"

This is the attempt of coding:

这是编码的尝试:

import numpy as np
twg=np.genfromtxt(r'./Documents/gears.txt', delimiter=',',dtype=(int,int,int,int,object))
print (twg)

But this eats up the #-sign in the source:

但这会消耗源中的#-sign:

[( 1,  85, 104,   2, b'"C"') ( 2,  71,  82,   2, b'"C')
 ( 3,  67,  73,   2, b'"D"') ( 4, 105, 108,   2, b'"D')
 ( 5, 103, 100,   2, b'"E"')]

1 个解决方案

#1


1  

The # is treated as a comment flag. Quoting doesn't make a difference:

#被视为注释标志。引用没有区别:

In [345]: txt=b'''1,85,104,2,"C"
     ...: 2,71,82,2,"C#"
     ...: 3,67,73,2,"D"
     ...: 4,105,108,2,"D#"
     ...: 5,103,100,2,"E"'''
In [346]: 
In [346]: np.genfromtxt(txt.splitlines(),delimiter=',',dtype=None)
Out[346]: 
array([(1,  85, 104, 2, b'"C"'), (2,  71,  82, 2, b'"C'),
       (3,  67,  73, 2, b'"D"'), (4, 105, 108, 2, b'"D'),
       (5, 103, 100, 2, b'"E"')],
      dtype=[('f0', '<i4'), ('f1', '<i4'), ('f2', '<i4'), ('f3', '<i4'), ('f4', 'S3')])

Turning off comments:

关闭评论:

In [347]: np.genfromtxt(txt.splitlines(),delimiter=',',dtype=None, comments=None
     ...: )
Out[347]: 
array([(1,  85, 104, 2, b'"C"'), (2,  71,  82, 2, b'"C#"'),
       (3,  67,  73, 2, b'"D"'), (4, 105, 108, 2, b'"D#"'),
       (5, 103, 100, 2, b'"E"')],
      dtype=[('f0', '<i4'), ('f1', '<i4'), ('f2', '<i4'), ('f3', '<i4'), ('f4', 'S4')])

#1


1  

The # is treated as a comment flag. Quoting doesn't make a difference:

#被视为注释标志。引用没有区别:

In [345]: txt=b'''1,85,104,2,"C"
     ...: 2,71,82,2,"C#"
     ...: 3,67,73,2,"D"
     ...: 4,105,108,2,"D#"
     ...: 5,103,100,2,"E"'''
In [346]: 
In [346]: np.genfromtxt(txt.splitlines(),delimiter=',',dtype=None)
Out[346]: 
array([(1,  85, 104, 2, b'"C"'), (2,  71,  82, 2, b'"C'),
       (3,  67,  73, 2, b'"D"'), (4, 105, 108, 2, b'"D'),
       (5, 103, 100, 2, b'"E"')],
      dtype=[('f0', '<i4'), ('f1', '<i4'), ('f2', '<i4'), ('f3', '<i4'), ('f4', 'S3')])

Turning off comments:

关闭评论:

In [347]: np.genfromtxt(txt.splitlines(),delimiter=',',dtype=None, comments=None
     ...: )
Out[347]: 
array([(1,  85, 104, 2, b'"C"'), (2,  71,  82, 2, b'"C#"'),
       (3,  67,  73, 2, b'"D"'), (4, 105, 108, 2, b'"D#"'),
       (5, 103, 100, 2, b'"E"')],
      dtype=[('f0', '<i4'), ('f1', '<i4'), ('f2', '<i4'), ('f3', '<i4'), ('f4', 'S4')])