Home python output the number of rows in the

output the number of rows in the




Tell me, please, how to output the number of rows downloaded from the file? I wrote the following code, but it displays the contents of the lines. And I need quantity:

def file_load ():
  With Open ("proxy.txt") AS Proxy:
    IPS = [row.rstrip () for row in proxy]
  With Open ("User-Agents.txt") AS User_Agents:
    UA = [row.rstrip () for row in user_agents]
  With Open ("Referers.txt") AS Referers:
    Ref = [row.rstrip () for Row in Referers]
  Print ('Loaded:', IPS, 'Proxies,', UA, 'User-Agents,', Ref, 'Referers')

Answer 1, Authority 100%

To output the number of rows in the file, it is not necessary to save the rows themselves, it is enough just to count how many times the symbol of the new line is found in the text:

def circu_lines (FileName, chunk_size = 1 & lt; & lt; 13):
  With Open (FileName) AS File:
    Return Sum (Chunk.Count ('\ N')
          For Chunk in Iter (Lambda: File.Read (Chunk_size), ''))

file opens in text mode (the row translation is converted to '\ n' on all systems), read by 8K characters in each to the end of the file and the number '\ N' in each block is summed up to find the total number of rows.

Code assumes that all rows, including the latest , ends with a new string symbol as well as WC -L utility (Accepted on POSIX, otherwise, for example, think what will happen if you call CAT * .TXT ). If the last symbol is not a new line, the last line is not considered (you can add a unit in this case).

Having COUNT_LINES () Function, it is easy to get the desired output:

print ('loadd: {nproxies} proxies, {nuser_agents} user-agents,'
   '{nreferrers} referers'.format (
    NPROXIES = COUNT_LINES ('proxy.txt'),
    Nuser_Agents = Count_lines ('User-Agents.txt'),
    NreferRers = Count_lines ('Referers.txt')))

Answer 2, Authority 80%

If you feel Zen Paiton:

sum (1 for line in open ('file', 'r'))

In my opinion, this is what you need, easily understandable.

Answer 3, Authority 60%

Use Len

print ('loadd:', len (IPS), 'Proxies,', Len (UA), 'user -Agents, ', Len (Ref),' Referers')

Answer 4, Authority 20%

Display the number of rows using a regular expression. Peculiarity. If the last line is not empty, then you will withdraw the amount on 1 element less.

import re
# Wills all lines including empty
Len (Re.Findall (R "[\ N '] +?", Open (' Bash.txt '). Read ()))
# Wills quantity without empty lines
Len (Re.Findall (R "[\ N '] +", Open (' Bash.txt '). Read ()))

On large files to the entire file, it is not possible to immediately apply it, it is possible to check for empty lines line or reading the part of the file, then folding the length.

Sample text file

1. Sudo PIP3 Install Django-Markdown-Deux
2. Sudo PIP3 Install Django-Filter
3. Sudo PiP3 Install Sorl-Thumbnail
4. Sudo APT-Get Install Libjpeg62 Libjpeg62-Dev Zlib1g-Dev
5. Sudo PIP3 Install Pillow
6. Sudo APT-Get Install LibgraphicsMagick ++ - Dev
7. Sudo Apt-Get Install Libboost-Python1.40-Dev
8. Sudo APT-Get Install Imagemagick
9. Sudo Apt-Get Install Graphicsmagick
12. Sudo Apt-Get Install Libmagickwand-Dev
13. Sudo PiP3 Install Wand
15. Sudo Python3 Manage.py Makemigrations Thumbnail

How it works. In this case, the greed of the regular expression is disabled.

& gt; & gt; & gt; re.findall (r "[\ n '] +?", open (' bash.txt '). read ())
['\ n', '\ n', '\ n', '\ n', '\ n', '\ n', '\ n', '\ n', '\ n', '\ n' , '\ n', '\ n', '\ n', '\ n', '\ n']

Greed is enabled, because of greed, regular expressions \ n \ n will be together where there is nothing but hyphenation

& gt; & gt; & gt; re.findall (r "[\ n '] +", open (' bash.txt '). read ())
['\ n', '\ n', '\ n', '\ n', '\ n', '\ n', '\ n', '\ n', '\ n \ n \ n', '\ n', '\ n \ n', '\ n']

If we count in the standard way, we get including empty lines that an empty line is also a line. This method always counts the number of lines as we understand it.

& gt; & gt; & gt; len (open ('bash.txt'). readlines ())

The reason why the quantity is different

'sudo pip3 install django-markdown-deux \ nsudo pip3 install django-filter \ nsudo pip3 install sorl-thumbnail \ nsudo apt-get install libjpeg62 libjpeg62-dev zlib1g-dev \ nsudo pip3 install Pillow \ nsudo apt-get install libgraphicsmagick ++ - dev \ nsudo apt-get install libboost-python1.40-dev \ nsudo apt-get install imagemagick \ nsudo get install graphicsmagick \ n \ n \ nsudo apt-get install libmagickwand-dev \ nsudo pip3 install Wand \ n \ nsudo python3 manage.py makemigrations thumbnail '

is visible here, there is no line break at the end which counts.

Option to correct the number of lines for regular expressions, the file will have to be read a second time from the end.

from __future__ import with_statement #tell ()
with open ('bash.txt', "r") as f:
  f.seek (0, 2)
  fsize = f.tell ()
  f.seek (max (fsize-68, 0), 0)
  lines = f.readlines ()
lines [-1:]
if '\ n' in lines [-1:]:
  print ("no")
  print ("+ 1")

If there is no hyphen in the last line, then add plus 1, in this case, just output to the screen.

An alternative solution to the same problem is to read the file from the beginning line by line, then take the last line and check the

character in it

open ('bash.txt', "r"). readlines () [- 1:]

Answer 5, authority 20%

f = open ('proxy.txt')
i = 0
for line in f:
  i + = 1
print (i)

Answer 6

I had a task to count the number of non-blank lines in a file. I propose a code that normally solves a problem with lines, including the last non-empty one, without reading the file from the end:

def file_lines_amount (filename: str) - & gt; int:
  import re
  '' '
  Calculates the number of non-empty lines in a file.
  '' '
  with open (filename, 'r') as f:
    return len (re.findall (r ". + \ n *", f.read ()))
if __name__ == "__main__":
  import os
  # Update the directory with the script.
  abspath = os.path.abspath (__ file__)
  dname = os.path.dirname (abspath)
  os.chdir (dname)
  print (file_lines_amount ('lines.txt'))

Answer 7

A one-liner calling the wc utility:

& gt; & gt; & gt; int (subprocess.check_output (['wc', '-l', 'text.out']). split () [0])

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions