Python 3统计CSV中的行数
从2.7版迁移到Python3环境后,我在获取行数方面遇到了问题。几次尝试后,返回的行数给出一个。我如何避免不推荐警告:Python3中不推荐使用“U”模式Python 3统计CSV中的行数,python,python-3.x,csv,migration,Python,Python 3.x,Csv,Migration,从2.7版迁移到Python3环境后,我在获取行数方面遇到了问题。几次尝试后,返回的行数给出一个。我如何避免不推荐警告:Python3中不推荐使用“U”模式 input_file = open("test.csv","rU") reader_file = csv.reader(input_file) value = len(list(reader_file)) 在使用Python3的情况下,我尝试了以下方法,但我仍然
input_file = open("test.csv","rU")
reader_file = csv.reader(input_file)
value = len(list(reader_file))
在使用Python3的情况下,我尝试了以下方法,但我仍然坚持使用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)
从
“rb”
中删除“b”
。仍然会给我一个1您是否可以共享您的CSV文件的摘录?可能重复>>len(列表(CSV.reader(open(r'new.CSV')))
对我有用。你的文件只有一行。虽然我还是少了一行,但如果你读了csv并且跳过了不好的行,那么有没有办法让原始csv中的#行和数据帧后期读取中的#行都得到呢?
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)