简体   繁体   English

使用“writeLines”python 方法保存后的预期文本文件行

[英]Expected text file lines after saving with "writeLines" python method

I am trying to split a large CSV file into several parts as files with Python .我正在尝试将一个大型 CSV 文件拆分为几个部分作为Python文件。 as a first try, I read the first 261579 lines from the CSV dataset file using this part of the code:作为第一次尝试,我使用这部分代码从 CSV 数据集文件中读取了前261579行:

    for c in range(261579):
        line = datasetFile.readline()
        if len(line) == 0:print("empty line detected at : " ,c)
        lines.append(line)
    print("SAVING LINES ......")
    split = open(outputDirectoryName+"spilt" + str(x+1) +".csv","w")
    split.writelines(lines)
    print("SPLIT " + str(x+1) + " END with " ,str(len(lines)) , "lines .")

OK, for the moment, the code works well and shows me好的,目前,代码运行良好并向我展示

"SPLIT 1 END with 261579 lines." “以 261579 行拆分 1 结束。”

, But the problem is that when I open my file "Split1.csv" with notpad++, I only find 261575 instead of 261579 , it's a loss of data for 4 lines somewhere in the file. ,但问题是当我用 notpad++ 打开我的文件“Split1.csv”时,我只找到261575而不是261579 ,这是文件中某处4行数据丢失。

With this proportion, I want to know what exactly happens with the "file.writeLines (lines)" method when do we use it to save my data in a split file?有了这个比例,我想知道“file.writeLines(lines)”方法究竟发生了什么,我们什么时候用它来将我的数据保存在一个拆分文件中?

我有同样的问题,然后我发现我应该关闭我的文件。为你

split.close()

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

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