简体   繁体   English

Numpy genfromtxt:python 2 vs 3在阅读换行符上的差异

[英]Numpy genfromtxt: python 2 vs 3 differences in reading line breaks

I am reading a CSV file using Numpy's genfromtxt. 我正在使用Numpy的genfromtxt读取CSV文件。 Everything works as expected in Python 2.7 (Numpy 1.11.3, anaconda distribution), but it fails completely in Python 3.4.3 (Numpy 1.12.0, installed through Ubuntu package manager). 一切都能在Python 2.7(Numpy 1.11.3,anaconda发行版)中按预期工作,但在Python 3.4.3(通过Ubuntu软件包管理器安装的Numpy 1.12.0)中完全失败。

The expected result in Python 2.7 (all of the data was read correctly): Python 2.7中的预期结果(正确读取了所有数据):

>>> a = np.genfromtxt('data1.csv', delimiter=',', skip_header=1)
>>> a.shape
(1460, 3)

But in Python 3.4, the operation returns nothing: 但是在Python 3.4中,该操作未返回任何内容:

>>> np.genfromtxt('data1.csv', delimiter=',', skip_header=1)
__main__:1: UserWarning: genfromtxt: Empty input file: "data1.csv"
array([], dtype=float64)
>>> a.shape
(0,)

If I don't skip the header, then I (some?) data from the file as a single array, and most of the values are nan: 如果我不跳过标题,那么我会将文件中的数据作为单个数组(有些?),并且大多数值都是nan:

>>> a.shape
(2923,)
>>> a
array([     nan,      nan,      nan, ...,      nan,    1256.,  147500.])

The first few lines of the CSV file are... CSV文件的前几行是...

Id,GrLivArea,SalePrice
1,1710,208500
2,1262,181500
3,1786,223500
4,1717,140000
5,2198,250000
6,1362,143000

I don't see any other questions regarding this on this site or Google... am I missing something? 在此网站或Google上,我没有看到与此有关的其他任何问题...我遗漏了什么吗? The commands are identical. 这些命令是相同的。 I know the numpy versions are a bit different, but this is as close as I can (easily) get between the two Python distributions. 我知道numpy版本有所不同,但这与我在两个Python发行版之间(可以轻松获得)的距离非常接近。

I am on Linux (Ubuntu) now, but I have also recreated the problem on Windows. 我现在在Linux(Ubuntu)上,但是我也在Windows上重新创建了该问题。

Here was the problem: for some reason my CSV file contained only carriage returns (\\r) at the line ends: there were no line feeds (\\n) [I think Excel can be thanked for this]. 这就是问题所在:由于某种原因,我的CSV文件的行尾仅包含回车符(\\ r):没有换行符(\\ n)[我认为可以为此感谢Excel]。 I replaced the carriage returns with line feeds and it worked. 我用换行符替换了回车符,它可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM