我需要在('/ dir'/)空格之外獲得csv文件的長度。 我嘗試這樣做:在多個csv文件中計算行數,跳空行
import os, csv, itertools, glob
#To filer the empty lines
def filterfalse(predicate, iterable):
# filterfalse(lambda x: x%2, range(10)) --> 0 2 4 6 8
if predicate is None:
predicate = bool
for x in iterable:
if not predicate(x):
yield x
#To read each file in '/dir/', compute the length and write the output 'count.csv'
with open('count.csv', 'w') as out:
file_list = glob.glob('/dir/*')
for file_name in file_list:
with open(file_name, 'r') as f:
filt_f1 = filterfalse(lambda line: line.startswith('\n'), f)
count = sum(1 for line in f if (filt_f1))
out.write('{c} {f}\n'.format(c = count, f = file_name))
我得到我想要的輸出,可惜每個文件的長度(「/ DIR /」),包括空行。
要看到空行來從我讀file.csv
爲file.txt
和它看起來像這樣:
*text,favorited,favoriteCount,...
"Retweeted user (@user):...
'empty row'
Do Operators...*
謝謝,它部分工作(即我仍然可以找到一些空行) – user2278505