打开数据文件以读取该fi中包含的行时

2024-05-23 14:05:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用以下代码段将数据文件分为两部分:

def shuffle_split(infilename, outfilename1, outfilename2):

    with open(infilename, 'r') as f:
        lines = f.readlines()

    lines[-1] = lines[-1].rstrip('\n') + '\n'

    shuffle(lines)

    with open(outfilename1, 'w') as f:
        f.writelines(lines[:90000])
    with open(outfilename2, 'w') as f:
        f.writelines(lines[90000:])
    outfilename1.close()
    outfilename2.close()    
shuffle_split(data_file, training_file,validation_file)

运行此代码段会导致以下错误

in shuffle_split
with open(infilename, 'r') as f:
TypeError: coercing to Unicode: need string or buffer, file found

打开数据文件进行输入的方式有什么问题


Tags: writelinesclose数据文件defas代码段withopen