python - Python 3 计算 CSV 中的行数
问题描述
从 2.7 迁移后,我无法在 python 3 环境中获取行数。几次尝试后,返回的行数为 1。如何绕过 DeprecationWarning: 'U' mode is deprecated in python 3 ?
input_file = open("test.csv","rU")
reader_file = csv.reader(input_file)
value = len(list(reader_file))
在使用 python 3 的情况下,我尝试了以下方法,但我仍然坚持使用 1。
input_file = open("test.csv","rb")
reader_file = csv.reader(input_file)
value = len(list(reader_file))
解决方案
如果您使用的是 pandas,您可以轻松做到这一点,而无需太多编码内容。
import pandas as pd
df = pd.read_csv('filename.csv')
## Fastest would be using length of index
print("Number of rows ", len(df.index))
## If you want the column and row count then
row_count, column_count = df.shape
print("Number of rows ", row_count)
print("Number of columns ", column_count)
推荐阅读
- macos - 终端消息:.zshrc:1:没有这样的文件或目录:
- java - 在 Java 中从浮点变量的输出中删除小数
- laravel - Gmail 没有收到我的 Laravel 用户验证电子邮件
- python-3.x - 未能在 Scrapy 中将类作为参数传递
- ada - 通过强类型和运算符重载进行简单的单元检查
- node.js - 如何让我的 API 函数将“字符串”返回给 Redis SETEX 值
- reactjs - 如何从 React 中的 Map() 对象返回反应标签树?
- encryption - 使用带有公钥的 OpenPGP.js 加密字符串
- python - Pandas DataFrame - 从 .txt 文件创建多个列
- python - 如何替换包含 Python Panda 中某些单词的单元格中的值,例如 Excel 中的值?