相关疑难解决方法(0)

如何在Python中廉价地获得行数?

我需要在python中获取大文件(数十万行)的行数.记忆和时间方面最有效的方法是什么?

目前我这样做:

def file_len(fname):
    with open(fname) as f:
        for i, l in enumerate(f):
            pass
    return i + 1
Run Code Online (Sandbox Code Playgroud)

有可能做得更好吗?

python text-files line-count

931
推荐指数
18
解决办法
77万
查看次数

如何获取Pandas数据帧的行数?

我正在尝试用Pandas获取dataframe df的行数,这是我的代码.

方法1:

total_rows = df.count
print total_rows +1
Run Code Online (Sandbox Code Playgroud)

方法2:

total_rows = df['First_columnn_label'].count
print total_rows +1
Run Code Online (Sandbox Code Playgroud)

两个代码片段都给我这个错误:

TypeError:+:'instancemethod'和'int'的不支持的操作数类型

我究竟做错了什么?

python dataframe pandas

786
推荐指数
12
解决办法
154万
查看次数

在Python中,如果我在"with"块内返回,文件是否仍会关闭?

考虑以下:

with open(path, mode) as f:
    return [line for line in f if condition]
Run Code Online (Sandbox Code Playgroud)

文件是否会正确关闭,或者以return某种方式绕过上下文管理器

python return with-statement

222
推荐指数
4
解决办法
4万
查看次数

csv文件中的行计数

我可能犯了一个愚蠢的错误,但我找不到它的位置.我想计算我的csv文件中的行数.我写了这个,显然不起作用:我row_count = 0应该是400.干杯.

f = open(adresse,"r")
reader = csv.reader(f,delimiter = ",")
data = [l for l in reader]
row_count = sum(1 for row in reader)

print row_count
Run Code Online (Sandbox Code Playgroud)

python csv row count

19
推荐指数
2
解决办法
7万
查看次数

Python 3 计算 CSV 中的行数

从 2.7 迁移后,我无法在 python 3 环境中获取行数。经过多次尝试,返回的行数为 1。如何解决 DeprecationWarning: 'U' 模式在 python 3 中已弃用?

             input_file = open("test.csv","rU")
             reader_file = csv.reader(input_file)
             value = len(list(reader_file))
Run Code Online (Sandbox Code Playgroud)

在使用 python 3 的情况下,我尝试了以下方法,但我仍然坚持使用 1。

             input_file = open("test.csv","rb")
             reader_file = csv.reader(input_file)
             value = len(list(reader_file))
Run Code Online (Sandbox Code Playgroud)

python csv migration python-3.x

2
推荐指数
1
解决办法
7338
查看次数