[英]How do I print the total number of records from a csv file in python?
I have a csv and I need to be able to print the total number of records how is this done? 我有一个csv,我需要能够打印记录总数,这是怎么做的? I have tried using sum statements and count but nothing seems to be working
我尝试使用sum语句和计数,但是似乎没有任何作用
Try this: 尝试这个:
with open(adresse,"r") as f:
reader = csv.reader(f,delimiter = ",")
data = list(reader)
row_count = len(data)
print(row_count)
Did you use pandas to import the csv file? 您是否使用熊猫来导入csv文件?
If so, here are some quick and easy options for obtaining the record count: 如果是这样,这里有一些快速简单的选项来获取记录数:
df = pandas.read_csv(filename)
len(df)
df.shape[0]
df.index
Otherwise, an alternative solution if you used csv.reader(filename.csv)
is: 否则,如果使用
csv.reader(filename.csv)
则另一种解决方案是:
row_count = sum(1 for line in open(filename))
(this solution was originally suggested here ) (此解决方案最初是在这里建议的)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.