循环通过csv文件创建较小的文件以进行操作,循环不激活

时间:2017-11-16 15:26:08

标签: python loops csv loading file-writing

以下代码用于循环遍历大型csv文件,一次取20个观察块,并将它们加载到较小的文件中,该文件将用于地理编码。循环似乎没有运行,并且已经没有想法(没有超过第一个row_count被打印,除了结束)

SalesLoad = open('C:\Users\DillonSaxe\Real Estate\Chi address 2000-2015.csv')
Sales = csv.reader(SalesLoad)
Storage = []
row_count = sum(1 for row in Sales)
print row_count
for row in Sales:
    if Sales.line_num == 1:
        Header = row
        print header
    elif Sales.line_num%20 != 0 and Sales.line_num != row_count:
        Storage.append(row)
    else:
        Storage.append(row)
        print row
        TempLoad = open('C:\Users\DillonSaxe\Real Estate\Temp Geocode File.csv', 'wb')
        TempGeocode = csv.writer(TempLoad)
        TempGeocode.writerow(Header)
        for item in Storage:
            TempGeocode.writerow(item)
        TempLoad.close()
SalesLoad.close()
print 'Done'

2 个答案:

答案 0 :(得分:0)

row_count = sum(1 for row in Sales)将您的阅读器放在文件的底部。在该行之后添加SalesLoad.seek(0),或重构您的循环以不需要行数。

答案 1 :(得分:0)

您可以使用枚举来测试行号并迭代文件一次

  with open('C:\Users\DillonSaxe\Real Estate\Chi address 2000-2015.csv', 'rb') as f
    reader = csv.reader(f)
    storage = [line for idx, line in enumerate(reader) if idx%20]

  with open('C:\Users\DillonSaxe\Real Estate\Temp Geocode File.csv', 'wb') as f
    writer = csv.writer(f)
    writer.writerows(storage )

  print 'Done'