[PYTHON] 10. Counting the number of lines

10. Counting the number of lines

Count the number of lines. Use the wc command for confirmation.

Go

package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	//Specify the read file
	name := "../hightemp.txt"
	line := 0

	//Open the file to read
	f, err := os.Open(name)
	if err != nil {
		fmt.Printf("os.Open: %#v\n",err)
		return
	}
	defer f.Close() //Crease at the end

	//Create a scanner library
	scanner := bufio.NewScanner(f)

	//Read one line of data
	for scanner.Scan() {
		line++;
	}

	//Check if there was an error
	if err = scanner.Err(); err != nil {
		fmt.Printf("scanner.Err: %#v\n",err)
		return
	}

	//Show number of lines
	fmt.Println("Line",line)
}

python

import sys

line = 0

#Open file
with open("../hightemp.txt", "r") as f:
    #Read line by line
    for data in f:
        #Add the number of lines
        line += 1

#Show number of lines
print("Line",line)

Javascript

//Module loading
var fs = require("fs");
var readline = require("readline");

var line = 0;

//Create a stream
var stream = fs.createReadStream("../hightemp.txt", "utf8");

//Pass Stream to readline
var reader = readline.createInterface({ input: stream });

//Row read callback
reader.on("line", (data) => {
    line = line + 1
});

//Close callback
reader.on("close", function () {
    console.log("Line",line);
});

Summary

Finally, we entered "Chapter 2: UNIX Command Basics"! !! ..

I heard that I entered Chapter 2, so I finally changed the Python version setting to 3.7. It's just an IDE setting ... Find out where the settings are ... Excuse me.

While checking each language to read the file. Go, Python didn't bother me so much, Javascirpt. Oh oh. Something interesting. Is it necessary to be careful about the way of thinking because it is asynchronous?

Supplement

The variable name was fname in Go language, but IDE (Golang) says typo ?. I wonder if I'm grateful. For the time being, change it to name and avoid it.

Top

Recommended Posts

10. Counting the number of lines
Get the number of digits
Calculate the number of changes
Get the number of views of Qiita
Calculation of the number of Klamer correlations
Get the number of Youtube subscribers
Count / verify the number of method calls.
Count the number of characters with echo
Output the number of CPU cores in Python
Calculate the total number of combinations with python
Divide the string into the specified number of characters
Find the number of days in a month
Minimize the number of polishings by combinatorial optimization
Determine the number of classes using the Starges formula
The beginning of cif2cell
The meaning of self
the zen of Python
The story of sys.path.append ()
Revenge of the Types: Revenge of types
python beginners tried to predict the number of criminals
How to know the port number of the xinetd service
Projecet Euler 12 Find the number of divisors without division.
How to get the number of digits in Python
relation of the Fibonacci number series and the Golden ratio
Count the number of parameters in the deep learning model
Calculation of the minimum required number of votes from turnout
Try to estimate the number of likes on Twitter
Predict the number of people infected with COVID-19 with Prophet
Get the size (number of elements) of UnionFind in Python
Manage the package version number of requirements.txt with pip-tools
[Python] Get the number of views of all posted articles
Visualize the number of complaints from life insurance companies
Clustering G-means that automatically determines the number of clusters
Scraping the result of "Schedule-kun"
How to find the optimal number of clusters in k-means
Maya | Find out the number of polygons in the selected object
The story of building Zabbix 4.4
Towards the retirement of Python2
Examine the margin of error in the number of deaths from pneumonia
[Apache] The story of prefork
Compare the fonts of jupyter-themes
Analyzing data on the number of corona patients in Japan
About the ease of Python
Explain the code of Tensorflow_in_ROS
Count the number of characters in the text on the clipboard on mac
Get the number of specific elements in a python list
Python --Find out number of groups in the regex expression
Reuse the results of clustering
Try to improve the accuracy of Twitter like number estimation
[Homology] Count the number of holes in data with Python
How to increase the number of machine learning dataset images
Let's visualize the number of people infected with coronavirus with matplotlib
GoPiGo3 of the old man
[Nonparametric Bayes] Estimating the number of clusters using the Dirichlet process
Get the number of occurrences for each element in the list
Change the theme of Jupyter
The popularity of programming languages
Change the style of matplotlib
Visualize the orbit of Hayabusa2
About the components of Luigi
Connected components of the graph