Skip to main content
 首页 » 编程设计

python之在 'for' 循环期间创建并写入新的 csv 文件

2024年10月17日6lexus

下面的代码旨在创建一个名为“file-0.csv”的 csv 文件,并通过遍历 for 循环开始写入行,直到达到 100 行。当达到限制时,它应该停止写入“file-0.csv”,创建“file-1.csv”,并从它停止的地方继续 for 循环,开始写入“file-1.csv” -1.csv' 直到达到 100 行,依此类推直到 for 循环完成。

下面代码(完整且可执行)的实际行为是它按预期创建了新文件(总共 4 个),但它继续将所有行写入“file-0”....

##### Python 3.5 ##### 
 
import csv 
 
rowCounter     = 0 
fileCounter    = 0 
List_A         = ['a', 'b', 'c', 'd', 'e', 'f', 'g'] 
List_B         = ['a', 'b', 'c', 'd', 'e', 'f', 'g'] 
List_C         = ['a', 'b', 'c', 'd', 'e', 'f', 'g'] 
 
 
def new_file(): 
    global fileCounter 
    fileCounter += 1 
    with open('file-' + str(fileCounter) + '.csv', 'w') as csvfile: 
        rowWriter = csv.writer(csvfile, delimiter=',', quoting=csv.QUOTE_NONE) 
 
with open('file-' + str(fileCounter) + '.csv', 'w') as csvfile: 
    rowWriter = csv.writer(csvfile, delimiter=',', quoting=csv.QUOTE_NONE) 
 
    for word1 in List_A: 
        for word2 in List_B: 
            for word3 in List_C: 
                sentence = word1 + word2 + word3 
                rowWriter.writerow ([sentence]) 
 
                rowCounter += 1 
 
                if rowCounter == 100: 
                    new_file() 
                    rowCounter = 0 
                else: 
                    continue 

与上面相同的代码,但有大量注释:

##### Python 3.5 ##### 
 
###################### 
####### Setup ######## 
###################### 
 
### Use the CSV library 
import csv 
 
### Initialize counters 
rowCounter     = 0 
fileCounter    = 0 
 
### Create three lists of 'words' 
List_A         = ['a', 'b', 'c', 'd', 'e', 'f', 'g'] 
List_B         = ['a', 'b', 'c', 'd', 'e', 'f', 'g'] 
List_C         = ['a', 'b', 'c', 'd', 'e', 'f', 'g'] 
 
### Function/subroutine that creates new CSV file with incremented filename 
def new_file(): 
    ### Make the variable 'fileCounter' usable by the function 
    global fileCounter 
    ### Add 1 to 'fileCounter' 
    fileCounter += 1 
    ### Create new CSV file using the value of 'fileCounter' as part of the name 
    with open('file-' + str(fileCounter) + '.csv', 'w') as csvfile: 
        rowWriter = csv.writer(csvfile, delimiter=',', quoting=csv.QUOTE_NONE) 
 
 
###################### 
#### Main Program #### 
###################### 
 
### Create initial CSV file using the value of 'fileCounter' as part of the name 
with open('file-' + str(fileCounter) + '.csv', 'w') as csvfile: 
    ### Create writer object and define how it should behave 
    rowWriter = csv.writer(csvfile, delimiter=',', quoting=csv.QUOTE_NONE) 
 
    ### Create & Write lines ### 
    ### Nested 'for' loops to iterate through all combinations of words 
    for word1 in List_A: 
        for word2 in List_B: 
            for word3 in List_C: 
                ### Build our 'sentence' from the current iteration 
                sentence = word1 + word2 + word3 
                ### Write 'sentence' to file 
                rowWriter.writerow ([sentence]) 
 
                ### Increment row counter 
                rowCounter += 1 
 
                ### Check if value of rowCounter is 100 and if so, execute 
                ### 'new_file' and reset rowCounter to 0. If not, continue. 
                if rowCounter == 100: 
                    new_file() 
                    rowCounter = 0 
                else: 
                    continue 

我怀疑问题是“rowWriter”没有得到更新或正确传递回主循环,但我似乎无法弄清楚该怎么做(无论如何,我什至不确定是否就是这样) .

我已经尝试记录并使代码“通用”,以便其他人可以从任何答案中得到一些用处。非常感谢任何帮助。

请您参考如下方法:

离开 with block 会关闭文件。因此,new_file 函数只是打开并立即关闭一个文件。

您可以执行以下操作:

import csv 
 
rowCounter     = 0 
fileCounter    = 0 
List_A         = ['a', 'b', 'c', 'd', 'e', 'f', 'g'] 
List_B         = ['a', 'b', 'c', 'd', 'e', 'f', 'g'] 
List_C         = ['a', 'b', 'c', 'd', 'e', 'f', 'g'] 
 
# create file handle 
csvfile = open('file-' + str(fileCounter) + '.csv', 'w') 
 
rowWriter = csv.writer(csvfile, delimiter=',', quoting=csv.QUOTE_NONE) 
 
for word1 in List_A: 
    for word2 in List_B: 
        for word3 in List_C: 
            sentence = word1 + word2 + word3 
            rowWriter.writerow ([sentence]) 
 
            rowCounter += 1 
 
            if rowCounter == 100: 
                # close current filehandle 
                csvfile.close() 
                fileCounter += 1 
                # open new file 
                csvfile =  open('file-' + str(fileCounter) + '.csv', 'w') 
                rowWriter = csv.writer(csvfile, delimiter=',', quoting=csv.QUOTE_NONE) 
                rowCounter = 0 
 
# close file 
csvfile.close() 

或者定义一个函数:

import csv 
 
rowCounter     = 0 
fileCounter    = 0 
List_A         = ['a', 'b', 'c', 'd', 'e', 'f', 'g'] 
List_B         = ['a', 'b', 'c', 'd', 'e', 'f', 'g'] 
List_C         = ['a', 'b', 'c', 'd', 'e', 'f', 'g'] 
 
def new_writer( csvfile, counter ): 
     if csvfile:  
         csvfile.close() 
 
     # open new file 
     csvfile =  open('file-' + str(counter) + '.csv', 'w') 
     rowWriter = csv.writer(csvfile, delimiter=',', quoting=csv.QUOTE_NONE) 
 
     counter += 1 
 
     return rowWriter,csvfile,counter 
 
rowWriter, csvFile, fileCounter = new_writer( None, fileCounter ) 
 
for word1 in List_A: 
    for word2 in List_B: 
        for word3 in List_C: 
            sentence = word1 + word2 + word3 
            rowWriter.writerow ([sentence]) 
 
            rowCounter += 1 
 
            if rowCounter == 100: 
                # close current file and open a new one 
                rowWriter, csvfile, counter =  new_writer( csvfile, fileCounter )          
                rowCounter = 0 
 
# close file 
csvFile.close()