Python 使用chunksize从read\u sql\u查询读取数据时,在变量中保留计数

Python 使用chunksize从read\u sql\u查询读取数据时,在变量中保留计数,python,pandas,Python,Pandas,在上面的代码中,我如何获得一个变量中正在写入csv文件的行数?只需添加一个I=0,每次写入一行时,您都要执行I+=1@Carlo1585在使用open和in-for循环count+=1之前,我尝试了类似count=0的方法,但没有成功,我也尝试了count+=sum(1表示uu in chunks),但它总是给出1,但你把count+=1放在for的内部,作为你说的for的skip列的注释,如果你给我一个csv示例,我可以尝试它并提供帮助,但我不确定它的内容,但它应该是有效的 import pa

在上面的代码中,我如何获得一个变量中正在写入csv文件的行数?

只需添加一个I=0,每次写入一行时,您都要执行I+=1@Carlo1585在使用open和in-for循环count+=1之前,我尝试了类似count=0的方法,但没有成功,我也尝试了count+=sum(1表示uu in chunks),但它总是给出1,但你把count+=1放在for的内部,作为你说的for的skip列的注释,如果你给我一个csv示例,我可以尝试它并提供帮助,但我不确定它的内容,但它应该是有效的
import pandas as pd
dbcon = ... # whatever

with open("out.csv", "w") as fh:

    chunks = pd.read_sql_query("SELECT * FROM table_name", dbcon,chunksize=10000)
    next(chunks).to_csv(fh, index=False)  # write the first chunk with the column names,
                                          # but ignore the index (which will be screwed up anyway due to the chunking)
    for chunk in chunks:
        chunk.to_csv(fh, index=False, header=False) # skip the column names from now on