BKC*_*pri 2 python csv migration python-3.x
从 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)
小智 9
如果您使用的是熊猫,您可以轻松地做到这一点,而无需太多编码。
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)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7338 次 |
| 最近记录: |