2017-02-09 95 views
0

我想遍歷文本文件的每1000行。我曾經做過類似於數據庫的工作,並且我首先爲每1000行寫了一個新ID,並對它進行了迭代。現在我想用文本文件本身來做。有沒有一些pythonic的方式來做到這一點?我只到目前爲止。在Python中對每1000行文本文件進行迭代

import pandas as pd 

input_file = 'text.csv' 
my_input = pd.read_csv(input_file, sep = ';') 
length = my_input.shape[0] 
start = 0 
end = 999 
#for the length of the whole document take the lines in range(start,end) 
    do stuff 
    start =+ 1000 
    end =+ 1000 
+0

首先,您需要決定是否希望按原樣讀取文件,將其作爲csv文件讀取,還是使用其數據幀表示法。 – DeepSpace

+0

@DeepSpace我需要每一行的一些屬性,所以在某些時候我會需要數據框。但也許可能首先讀取1000行,然後創建一個數據框,以便我可以讀取屬性? – student

回答

0

它似乎與大火庫一起工作。

import pandas as pd 

input_file = 'text.csv' 
my_input = pd.read_csv(input_file, sep = ';', names=['a', 'b', 'c'] 
for chunk in blaze.odo(my_input, target=bz.chunks(pd.DataFrame), chunksize=1000): 
    for index, row in chunk.iterrows(): 
      variable1 = row['a'] 
      variable1 = row['b'] 
      do stuff