python csv2libsvm.py: AttributeError: '_csv.reader' 对象没有属性 'next'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42767250/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
python csv2libsvm.py: AttributeError: '_csv.reader' object has no attribute 'next'
提问by Zoya
I want to convert a csv file into sparse format file with csv2libsvm.py (https://github.com/zygmuntz/phraug/blob/master/csv2libsvm.py).
我想使用 csv2libsvm.py ( https://github.com/zygmuntz/phraug/blob/master/csv2libsvm.py)将 csv 文件转换为稀疏格式文件。
The CSV file contains 37 attributes + the label (last column). it doesn't contain header or index. Exp of the 1st row: 63651000000.0,63651000000.0,153.1,0,0,0,0,0,0,5,1,0,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
CSV 文件包含 37 个属性 + 标签(最后一列)。它不包含标题或索引。第一行的 Exp: 63651000000.0,63651000000.0,153.1,0,0,0,0,0,0,5,1,0,4,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
When entring the following command line : python csv2libsvm.py Z.csv data.txt 38 1
输入以下命令行时: python csv2libsvm.py Z.csv data.txt 38 1
I got the following error:
我收到以下错误:
Traceback (most recent call last):
File "csv2libsvm.py", line 47, in <module>
headers = reader.next()
AttributeError: '_csv.reader' object has no attribute 'next'
Do you have any idea about the problem ?
你对这个问题有什么想法吗?
回答by Hossein
This is because of the differences between python 2 and python 3. Use the built-in function next
in python 3. That is, write next(reader)
instead of reader.next()
in line 47. In addition, you should open the file in the text mode. So, change line 47 as i = open( input_file, 'r' )
.
这是因为python 2和python 3的不同。使用python 3中的内置函数next
。即写next(reader)
而不是reader.next()
在第47行。另外,您应该以文本模式打开文件。因此,将第 47 行更改为i = open( input_file, 'r' )
。
回答by Solution Spirit
For Python 3.x:
对于 Python 3.x:
Use next(reader)
instead of reader.next()
使用next(reader)
代替reader.next()