Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 属性错误:'_io.TextIOWrapper';对象没有属性';下一个';_Python_Csv - Fatal编程技术网

Python 属性错误:'_io.TextIOWrapper';对象没有属性';下一个';

Python 属性错误:'_io.TextIOWrapper';对象没有属性';下一个';,python,csv,Python,Csv,使用我在Python 2中编写的函数,我尝试连接csv文件: def concat_csv(): """ A function to concatenate the previously wrangled csv files in current working dir """ with open('2017_oldnew.csv', 'a') as f_out: # First file with open('old.csv')

使用我在Python 2中编写的函数,我尝试连接csv文件:

def concat_csv():
    """
    A function to concatenate the previously wrangled csv files in current working dir
    """
    with open('2017_oldnew.csv', 'a') as f_out:
        # First file
        with open('old.csv') as f_read:
            for line in f_read:
                f_out.write(line)
        # Second file
        with open('new.csv') as f_read:
            f_read.next()
            for line in f_read:
                f_out.write(line)
但是,运行Python 3会给我一条错误消息:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-110-a5a430e1b905> in <module>()
      1 # Concatenate the files
----> 2 concat_csv()

<ipython-input-109-9a5ae77e9dd8> in concat_csv()
     10         # Second file
     11         with open('new.csv') as f_read:
---> 12             f_read.next()
     13             for line in f_read:
     14                 f_out.write(line)

AttributeError: '_io.TextIOWrapper' object has no attribute 'next'
---------------------------------------------------------------------------
AttributeError回溯(最近一次呼叫上次)
在()
1#连接文件
---->2 concat_csv()
在concat_csv()中
10#第二个文件
11打开('new.csv'),如f_所示:
--->12 f_读取下一页()
13对于f_中的行,读取:
14 f_out.写入(行)
AttributeError:“\u io.TextIOWrapper”对象没有属性“next”

事实证明,在Python3中,语法发生了变化。我们不需要使用next作为方法,而是需要将其作为函数使用,如下所示:

next(f_read)

这会立即解决问题。

更具体地说,迭代器有一个名为
\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu。这使它与其他协议(例如,
len
调用
\uuu len\uuu
等)保持一致。注意:,因此您可以在2.6+和3.x上使用此代码。从2到3的实际变化是,特殊方法的名称从
next
更改为
\uuuuuuuuuuuuu
(以匹配其他特殊方法,并避免在
next
作为方法有意义但您的类不是迭代器的情况下导致问题)。使用
下一步
功能文件进行更改;它在两个版本上都能正常工作。