当我将一个excel文件传递给计算文件中行数的python代码时,实际计算的是什么?

2024-03-29 09:19:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用了以下问题作为来源,并列出了我要问的代码:


  1. How to get line count cheaply in Python?

    def file_len(fname):
        with open(fname) as f:
            for i, l in enumerate(f):
                pass
        return i + 1
    
    fname = '/path/file.xls'
    file_len(fname)
    

当实际行数为56时,返回40。你知道吗


  1. How to count rows in multiple csv file

    import glob
    import pandas as pd
    
    files = glob.glob('files/*.xls')
    
    d = {f: sum(1 for line in open(f)) for f in files}
    

对于同一个文件,它也返回了40,并且也没有为该路径中的其他文件返回正确的计数。你知道吗


它不返回错误,并且在方法之间一致地返回相同的计数,但是行数不正确。你知道吗

我的问题是:在.xls文件中实际计算的是什么,因为它不是行数?你知道吗


Tags: 文件toinforlenascountline