Python 3计算CSV中的行数

时间:2019-06-04 00:35:29

标签: python python-3.x csv migration

从2.7迁移后,在python 3环境中获取行数有麻烦。经过几次尝试后,返回的行数为1。我该如何避免DeprecationWarning:Python 3中不推荐使用'U'模式?

             input_file = open("test.csv","rU")
             reader_file = csv.reader(input_file)
             value = len(list(reader_file))

在使用python 3的情况下,Ive尝试了以下方法,但我仍然坚持使用1.。

             input_file = open("test.csv","rb")
             reader_file = csv.reader(input_file)
             value = len(list(reader_file))

2 个答案:

答案 0 :(得分:1)

如果您使用的是熊猫,则无需太多编码即可轻松实现。

import 'react-native'
import React from 'react'
import renderer from 'react-test-renderer'

import NoResults from './NoResults'

jest.mock('react-instantsearch-native')

it('renders correctly when searching', () => {
  const tree = renderer
    .create(<NoResults searching searchState={{ query: '' }} />)
    .toJSON()
  expect(tree).toMatchSnapshot()
})

答案 1 :(得分:0)

input_file = open("test.csv","rb") #rb is a read-in-binary format and 
#you can't count the number of row from binary format file

with open("text.csv",'r') as f:
file = f.readlines()
print(len(file))

# Data in my text file
# a
# b
# c
# d
# e

#The output of above code is 
#5 means number of rows is 5