numy使用
调用numy函数,想把数据以矩阵形式读出来,数据之间的逗号用中文会报错,英文逗号不会,这是为什么?#numpy 读写以矩阵形式
import numpy
a=numpy.genfromtxt("C:\\Users\\lzj\\Desktop\\text.txt",delimiter=",",dtype=str)
print(a)
报错如下:
ValueError Traceback (most recent call last)
<ipython-input-99-0853a2d82c3b> in <module>()
1 #numpy 读写以矩阵形式
2 import numpy
----> 3 a=numpy.genfromtxt("C:\\Users\\lzj\\Desktop\\text.txt",delimiter=",",dtype=str)
4 print(a)
~\Anaconda3\lib\site-packages\numpy\lib\npyio.py in genfromtxt(fname, dtype, comments, delimiter, skip_header, skip_footer, converters, missing_values, filling_values, usecols, names, excludelist, deletechars, replace_space, autostrip, case_sensitive, defaultfmt, unpack, usemask, loose, invalid_raise, max_rows, encoding)
2029 # Raise an exception ?
2030 if invalid_raise:
-> 2031 raise ValueError(errmsg)
2032 # Issue a warning ?
2033 else:
ValueError: Some errors were detected !
Line #2 (got 4 columns instead of 3)
import numpy
a = numpy.genfromtxt("test.txt", delimiter=",", dtype=str, encoding='utf-8') # 以中文逗号分隔,得修改默认encoding。
print(a)
txt,csv 啥的一般默认是英文的逗号进行分隔
页:
[1]