AttributeError:'_ io.TextIOWrapper'对象没有属性'next'

时间:2017-03-28 21:40:58

标签: 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') 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'

1 个答案:

答案 0 :(得分:3)

事实证明,在Python 3中,语法已经改变。我们需要将其用作以下函数,而不是使用next作为方法:

next(f_read)

立即修复问题。