在Python中对每1000行文本文件进行迭代
问题描述:
我想遍历文本文件的每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
答
它似乎与大火库一起工作。
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
首先,您需要决定是否希望按原样读取文件,将其作为csv文件读取,还是使用其数据帧表示法。 – DeepSpace
@DeepSpace我需要每一行的一些属性,所以在某些时候我会需要数据框。但也许可能首先读取1000行,然后创建一个数据框,以便我可以读取属性? – student