【问题标题】:The file_counter is not updating when the script run, I want the file_counter to update so that a new csv file can be created脚本运行时 file_counter 未更新,我希望 file_counter 更新以便可以创建新的 csv 文件
【发布时间】:2021-06-19 01:44:40
【问题描述】:

file_counter 没有更新,我希望 file_counter 更新,以便在旧 CSV 文件达到设置的限制后创建新的 CSV 文件。我想在 CSV 文件达到我设置的限制时自动创建一个 CSV 文件

 file_counter = 1
 data_file_exists = os.path.isfile('data'+str(file_counter)+'.csv')
 data_file = open('data'+str(file_counter)+'.csv', 'a', encoding='utf-8', newline='\n')
 writer = csv.writer(data_file, delimiter=',')
 data_f  = open('data'+str(file_counter)+'.csv', 'r', encoding='utf-8', newline='\n')    
 reader = csv.reader(data_file, delimiter=',')
 count = 0
 for lines in data_f:
     count += 1
 print(count)
 while not count <= 20:
     file_counter += 1
 print(file_counter)

【问题讨论】:

    标签: python selenium csv web-scraping web-crawler


    【解决方案1】:

    你的逻辑有缺陷。

    while not count <= 20:
         file_counter += 1
    

    应该是:

    while count <= 20:
         file_counter += 1
    

    即使您使用 while 循环将 count 设置为任何数字,它也不会增加 file_counter。
    使用我给你的while循环,它将无限循环。如果您尝试将限制设置为 20,我建议使用 if 语句来检查 count 是否已达到限制。如果有,您可以创建一个新文件。

    If count >= 20:
        create_new_file()
    

    【讨论】:

      猜你喜欢
      • 2016-07-08
      • 2012-08-11
      • 1970-01-01
      • 2012-07-27
      • 1970-01-01
      • 1970-01-01
      • 2016-01-15
      • 2015-12-06
      • 1970-01-01
      相关资源
      最近更新 更多