[PYTHON] 100 amateur language processing knocks: 10

It is a challenge record of Language processing 100 knock 2015. The environment is Ubuntu 16.04 LTS + Python 3.5.2 : : Anaconda 4.1.1 (64-bit). Click here for a list of past knocks (http://qiita.com/segavvy/items/fb50ba8097d59475f760).

Chapter 2: UNIX Command Basics

hightemp.txt is a file that stores the record of the highest temperature in Japan in the tab-delimited format of "prefecture", "point", "℃", and "day". Create a program that performs the following processing and execute hightemp.txt as an input file. Furthermore, execute the same process with UNIX commands and check the execution result of the program.

First, Japaneseize man

From this chapter you will learn UNIX commands. I didn't know how to look up UNIX commands, so when I asked a colleague at work, he advised me to "get a habit of looking up with man first" and immediately tried man wc on the terminal.

Kobito.W1nTgc.png

Was it still in English?> <

But thankfully there was a package that was translated into Japanese. sudo apt-get install manpages-ja You can install it with. Thank you to all the volunteers.

Now I'm in Japanese.

Kobito.z0ZAzE.png

Let's move on to the problem.

10. Counting the number of lines

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

The finished code:

main.py


# coding: utf-8

fname = 'hightemp.txt'
count = 0
with open(fname) as data_file:
	for line in data_file:
		count += 1
print(count)

Execution result:

Terminal


24

For confirmation with UNIX commands, I will try using a script as well as studying shell scripts.

test.sh


#!/bin/sh

wc --line hightemp.txt

Execution result:

Terminal


24 hightemp.txt

The number of lines is the same.

Precautions when executing a shell script (difference from Windows batch file)

When executing a shell script directly, it is necessary to give execute permission to the script file. Grant permissions with chmod + x test.sh etc. Also, UNIX will not find the current executable if it is not in the path. At runtime, you need to specify the location like ./test.sh. Going back and forth between UNIX and Windows can be confusing. It may be a matter of familiarity, but sometimes I type ls on Windows ^^;   That's all for the 11th knock. If you have any mistakes, I would appreciate it if you could point them out.

Recommended Posts

100 amateur language processing knocks: 41
100 amateur language processing knocks: 71
100 amateur language processing knocks: 56
100 amateur language processing knocks: 24
100 amateur language processing knocks: 59
100 amateur language processing knocks: 70
100 amateur language processing knocks: 60
100 amateur language processing knocks: 92
100 amateur language processing knocks: 30
100 amateur language processing knocks: 06
100 amateur language processing knocks: 84
100 amateur language processing knocks: 81
100 amateur language processing knocks: 33
100 amateur language processing knocks: 40
100 amateur language processing knocks: 45
100 amateur language processing knocks: 43
100 amateur language processing knocks: 55
100 amateur language processing knocks: 22
100 amateur language processing knocks: 61
100 amateur language processing knocks: 94
100 amateur language processing knocks: 54
100 amateur language processing knocks: 04
100 amateur language processing knocks: 63
100 amateur language processing knocks: 12
100 amateur language processing knocks: 14
100 amateur language processing knocks: 08
100 amateur language processing knocks: 42
100 amateur language processing knocks: 19
100 amateur language processing knocks: 73
100 amateur language processing knocks: 75
100 amateur language processing knocks: 98
100 amateur language processing knocks: 83
100 amateur language processing knocks: 95
100 amateur language processing knocks: 32
100 amateur language processing knocks: 96
100 amateur language processing knocks: 87
100 amateur language processing knocks: 72
100 amateur language processing knocks: 23
100 amateur language processing knocks: 05
100 amateur language processing knocks: 00
100 amateur language processing knocks: 02
100 amateur language processing knocks: 37
100 amateur language processing knocks: 21
100 amateur language processing knocks: 68
100 amateur language processing knocks: 11
100 amateur language processing knocks: 90
100 amateur language processing knocks: 74
100 amateur language processing knocks: 66
100 amateur language processing knocks: 28
100 amateur language processing knocks: 64
100 amateur language processing knocks: 34
100 amateur language processing knocks: 36
100 amateur language processing knocks: 77
100 amateur language processing knocks: 01
100 amateur language processing knocks: 16
100 amateur language processing knocks: 27
100 amateur language processing knocks: 10
100 amateur language processing knocks: 03
100 amateur language processing knocks: 82
100 amateur language processing knocks: 69
100 amateur language processing knocks: 53