python 循环读取表格每次读入N行

Python013

python 循环读取表格每次读入N行,第1张

from itertools import islice

N = 1000000 #每次读入100w行

with open( 'your_File_path','r') as f:

while True:

next_n_lines = list(islice(f,N))

if not next_n_lines: break

#print(str(next_n_lines))

#将每次读入的n行记录转换成DataFrame处理

next_n_lines="".join(next_n_lines)

TESTDATA = StringIO(next_n_lines)

df = pd.read_csv(TESTDATA, sep="\t",header=None,names=['col_1','col_2','col_Name'],dtype=str)

print(df)

time.sleep(1)

总共六个字符

python可以通过str*2重复输出字符串printstr*2#输出字符串两次,循环代码块就是将取到的每个字符进行打印输出,总共六个字符,就循环执行了6次。接下来我们使用For循环遍历一个列表。