Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Get number of lines in file

Reply
Thread Tools

Get number of lines in file

 
 
ssmith579@aol.com
Guest
Posts: n/a
 
      05-27-2005
I have read in a file and need to get the number of lines.

cpn_file = open('Central Part number list.txt')
cpn_version = cpn_file.read().split('\n')

I want to know the number of elements in cpn_version.

 
Reply With Quote
 
 
 
 
Elliot Temple
Guest
Posts: n/a
 
      05-27-2005

On May 27, 2005, at 12:17 PM, http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

> I have read in a file and need to get the number of lines.
>
> cpn_file = open('Central Part number list.txt')
> cpn_version = cpn_file.read().split('\n')
>
> I want to know the number of elements in cpn_version.


Could you use:

count_lines = len(cpn_file.readlines())

-- Elliot Temple
http://www.curi.us/


---
[This E-mail scanned for viruses by Declude Virus]

 
Reply With Quote
 
 
 
 
ssmith579@aol.com
Guest
Posts: n/a
 
      05-27-2005
Thanks! I was trying len(cpn_version) and that didn't work.

 
Reply With Quote
 
Steven Bethard
Guest
Posts: n/a
 
      05-28-2005
Elliot Temple wrote:
>
> On May 27, 2005, at 12:17 PM, (E-Mail Removed) wrote:
>
>> I have read in a file and need to get the number of lines.
>>
>> cpn_file = open('Central Part number list.txt')
>> cpn_version = cpn_file.read().split('\n')
>>
>> I want to know the number of elements in cpn_version.

>
> Could you use:
>
> count_lines = len(cpn_file.readlines())


Or if you're worried about reading all of cpn_file into memory at once,
you could try something like:

sum(1 for line in cpn_file)

or in Python 2.3:

sum([1 for line in cpn_file])

STeVe
 
Reply With Quote
 
Magnus Lycka
Guest
Posts: n/a
 
      05-30-2005
(E-Mail Removed) wrote:
> Thanks! I was trying len(cpn_version) and that didn't work.


What's your problem? You get a value that's one more than
you expected? You should use splitlines() instead of split('\n'),
or easier, use readlines() instead of read(). Of course, with
a modern python you can just iterate over the file, but note
the difference between split and splitlines when the last line
is complete and ends with a newline character:

>>> a = """dgdgsdfg

.... sdfgsdfgsdfg
.... sdfgsdfgsdfg
.... sdfgsdfgsdfg
.... """
>>> a

'dgdgsdfg\nsdfgsdfgsdfg\nsdfgsdfgsdfg\nsdfgsdfgsdf g\n'
>>> a.split('\n')

['dgdgsdfg', 'sdfgsdfgsdfg', 'sdfgsdfgsdfg', 'sdfgsdfgsdfg', '']
>>> a.splitlines()

['dgdgsdfg', 'sdfgsdfgsdfg', 'sdfgsdfgsdfg', 'sdfgsdfgsdfg']
>>>


If you're allergic to splitlines you could do...

>>> a.rstrip().split('\n')

['dgdgsdfg', 'sdfgsdfgsdfg', 'sdfgsdfgsdfg', 'sdfgsdfgsdfg']

....but it depends how you want to view files that end with
several linefeeds in a row (or other whitespace for that
matter).

>>> a = """"dfgdfg

.... dfgdfg
....
.... dgfdfg
....
....
....
.... """
>>> a.split('\n')

['"dfgdfg', 'dfgdfg', '', 'dgfdfg', '', '', '', '']
>>> a.splitlines()

['"dfgdfg', 'dfgdfg', '', 'dgfdfg', '', '', '']
>>> a.rstrip().split('\n')

['"dfgdfg', 'dfgdfg', '', 'dgfdfg']

In other words, the right solution depends on what behaviour
you want for such cases (if they might exist with your files).

Experimenting like this with the interpreter is a very
convenient way to get a grip on things in Python, and one of
the reasons that Python debugging is usually quicker than
debugging in other languages.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
new topic: I call length($<string>) and get number of lines - codefrag below - on MAC OS X 10.7 kquirici Perl Misc 13 06-19-2012 10:20 AM
To delete few lines and add few lines at the end of a text file using c program Murali C++ 2 03-09-2006 04:45 PM
OT: Number Nine, Number Nine, Number Nine FrisbeeŽ MCSE 37 09-26-2005 04:06 PM
How to get the total number of lines of a text file Mullin Java 4 06-12-2005 09:15 PM
Re: how to read 10 lines from a 200 lines file and write to a new file?? Joe Wright C Programming 0 07-27-2003 08:50 PM



Advertisments