我需要在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)
有可能做得更好吗?
我正在尝试用Pandas获取dataframe df的行数,这是我的代码.
total_rows = df.count
print total_rows +1
Run Code Online (Sandbox Code Playgroud)
total_rows = df['First_columnn_label'].count
print total_rows +1
Run Code Online (Sandbox Code Playgroud)
两个代码片段都给我这个错误:
TypeError:+:'instancemethod'和'int'的不支持的操作数类型
我究竟做错了什么?
考虑以下:
with open(path, mode) as f:
return [line for line in f if condition]
Run Code Online (Sandbox Code Playgroud)
文件是否会正确关闭,或者以return某种方式绕过上下文管理器?
我可能犯了一个愚蠢的错误,但我找不到它的位置.我想计算我的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) 从 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 ×5
csv ×2
count ×1
dataframe ×1
line-count ×1
migration ×1
pandas ×1
python-3.x ×1
return ×1
row ×1
text-files ×1