Reading and writing text in Python

This time too, I will talk about reading and writing text in Python, which I often use.

1. Sample code

sample.py


#-*- coding:utf-8 -*-

if __name__ == '__main__':
	print "\ntest.Create txt and start a new line 1~Let me enter up to 100."
	fw = open("test.txt","w")
	for line in range(0,100):
		number = line + 1
		if line == 0:	fw.write("%s"%number)
		else: fw.write("\n%s"%number)
	fw.close()
	print "Input is complete.\n"

	print "Then test2.Create txt and open the feeling with tabs 1~Let me enter up to 100."
	fw2 = open("test2.txt","w")
	for line2 in range(0,100):
		number = line2 + 1
		if line2 == 0:	fw2.write("%s"%number)
		else: fw2.write("\t%s"%number)
	fw2.close()
	print "Input is complete.\n"

	print "test.The number of lines entered in txt will be displayed from now on."
	fr = open("test.txt","r")
	for line in fr:
		len_item = sum(1 for line in fr)
		print "In the text%There are s lines.\n"%(len_item+1)
	fr.close()

	print "test2.I will display what I entered in txt."
	fr2 = open("test.txt","r")
	for line2 in fr2:
		print line2,
	print ''
	fr2.close()	
	print "The display is finished.\n"

	print "That's all for this program. Thank you for your hard work.\n"

2. Explanation?

I put a little small material in various ways. For example, the number of lines entered in " test.txt will be displayed from now on. The way to write sum (1 for line in fr) written in ". This will tell you how many lines there are in the file. Moreover, it seems that writing with sum (1 for line in fr) will calculate faster than finding it by turning it in the outside for line in fr.

In addition, I will display what I entered in `" test2.txt. "

print ''```
 That part. By writing this, you can print the elements one by one.


# 3. Introduction of the site that was taken care of
 A very helpful site: "[Read a file with Python](http://osksn2.hep.sci.osaka-u.ac.jp/~taku/osx/python/readfile.html)"
 There are many samples and it is very useful m (_ _) m


Recommended Posts

Reading and writing text in Python
Reading and writing CSV and JSON files in Python
Python CSV file reading and writing
Reading and writing NetCDF with Python
Reading and writing CSV with Python
Notes on reading and writing float32 TIFF images in python
Reading and writing JSON files with Python
Clustering text in Python
Text processing in Python
Study from Python Reading and writing Hour9 files
Reading and writing fits files with Python (memo)
Example of reading and writing CSV with Python
Reading from text files and SQLite in Python (+ Pandas), R, Julia (+ DataFrames)
UTF8 text processing in python
Reading and writing csv files
Stack and Queue in Python
Speech to speech in python [text to speech]
Unittest and CI in Python
Python text reading for multiple lines and one line
[Introduction for beginners] Reading and writing Python CSV files
I tried [scraping] fashion images and text sentences in Python.
MIDI packages in Python midi and pretty_midi
Difference between list () and [] in Python
View photos in Python and html
Sorting algorithm and implementation in Python
Manipulate files and folders in Python
About dtypes in Python and Cython
Assignments and changes in Python objects
Check and move directories in Python
Extract text from images in Python
Ciphertext in Python: IND-CCA2 and RSA-OAEP
Sort large text files in Python
Hashing data in R and Python
Function synthesis and application in Python
Export and output files in Python
Reverse Hiragana and Katakana in Python2.7
[GUI in Python] PyQt5-Menu and Toolbar-
When writing a program in Python
Create and read messagepacks in Python
uproot: Python / Numpy based library for reading and writing ROOT files
Overlapping regular expressions in Python and Java
Differences in authenticity between Python and JavaScript
Notes using cChardet and python3-chardet in Python 3.3.1.
Modules and packages in Python are "namespaces"
Avoid nested loops in PHP and Python
Differences between Ruby and Python in scope
AM modulation and demodulation in Python Part 2
difference between statements (statements) and expressions (expressions) in Python
Eigenvalues and eigenvectors: Linear algebra in Python <7>
Implementation module "deque" in queue and Python
Line graphs and scale lines in python
Implement FIR filters in Python and C
Check and receive Serial port in Python (Port check)
Search and play YouTube videos in Python
Difference between @classmethod and @staticmethod in Python
Difference between append and + = in Python list
Difference between nonlocal and global in Python
Write O_SYNC file in C and Python
Template for writing batch scripts in python
Dealing with "years and months" in Python
(Python) HTML reading and regular expression notes