Starfruit Python Advanced Lecture 5-Input and Read Files

My CSDN blog column: https://blog.csdn.net/yty_7

Github address: https://github.com/yot777/

 

Speaking of the equal sign and assignment number last time. Why emphasize the order of assignment from right to left ? Because our Chinese from the novel's Chinese is a standard from left to right, from large to small language structure, that is, SVO (Subject Verb Object) structure, that is, subject-predicate-object structure, so it is not easy for us to understand from right to left Order, but in fact, right-to-left usage in many languages ​​in the world, such as how to write the address of the English letter, do you remember?

No. 888, Unit 2, Community A, Huawei South Road, Chaoyang District, Beijing, China->

Room.888,Unit 2,Community A,South Huawei Rd.,Chaoyang District,Beijing,P.R.China.

This is how to write from small to large, and you need to read from right to left.

In addition, the structure of Japanese and Korean is SOV (Subject Object Verb), which is subject-object predicate, so you need to read from right to left.

So please remember that the assignment numbers are from right to left.

 

5.1 Python input:

Python input is very simple, just use the input () function. There are two points to note:

1. The input () function has no parameters, but you can add a string as an input prompt.

2. No matter what you type from the keyboard, Python considers it to be a string type, so in some cases you need to convert the data type.

Python single line input example:

#直接将input()函数输入的内容赋值给变量t
>>> t = input("Please input:")   
Please input:6
>>> t
'6'    #注意变量t现在是字符'6'不是数字6
>>> s = t + 7
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: must be str, not int   #报错,字符'6'无法直接和数字7相加
>>>
#将input()函数输入的内容转型为int(整型)之后再赋值给变量t
>>> t = int(input("Please input:"))  
Please input:6
>>> t
6     #变量t现在是数字6
>>> s = t + 7   #可以正常执行整数的加法
>>> print(s)
13

There are many insurmountable problems with single-line input, such as the inability to enter newline content (even if \ n is not enough), and the list of multiple elements.

>>> t = input("Please input:")
Please input:[0,1,2]   #尝试输入一个[0,1,2]列表
>>> t
'[0,1,2]'     #被解析为了'[0,1,2]'字符串
>>> t[0]
'['           #'[0,1,2]'字符串的第1个元素是'[' 
>>> t[1]
'0'           #'[0,1,2]'字符串的第2个元素是'0' 
>>> t[2]
','           #'[0,1,2]'字符串的第3个元素是',' 
>>> t = list(input("Please input:"))  #即使被强制转型为list类型也不行
Please input:[0,1,2]   
>>> t[0]
'['
>>> t[1]
'0'
>>> t[2]
','
>>> t = input("input:")
input:a\nb\nc      #原本是想用\n作为换行符输出3行
>>> t
'a\\nb\\nc'

5.2 Python read files:

Since the input () function does not support multi-line input, we can use a workaround to solve this problem: read the file!

Reading the file is divided into the following steps:

1. Open the file and use the open () function. The complete function is:

f = open('test.txt', 'r')

The first parameter of the open function is the file name, and the second parameter indicates the file type: r indicates a text file, and rb indicates a binary file

The second parameter can be omitted, the default is a text file

The return value of the open function is a file object . Note that it is an instantiated object! This means it can have its own method.

2. To read the file, use the read () or readline () method, the difference between them is:

read () reads the entire file, it is usually used to put the entire file content into a string variable;

readline () reads only one line, then puts the line into a string variable;

readlines () reads the entire file and automatically breaks the contents of the file into a list containing all the lines.

In practical applications, readlines is more widely used than read and readlines, and it is more convenient for subsequent processing to automatically decompose the contents of the file into a list containing all lines.

lines = f.readlines()

3. After reading, use the close () method to close the file to prevent the file object from being occupied all the time without releasing the memory.

f.close()

Let's test an example, assuming the following test.txt file:

#test.txt文件
1	2	3	4	5
6	7	8	9	10
11	12	13	14	15
16	17	18	19	20

There are 5 elements per line, separated by Tab key, there are 4 lines in total, we directly use the readlines () method:

#读取文件试验1
#打开原始文件
>>> f = open('test.txt')
#readlines() 读取整个文件,自动将文件内容分析成一个包含所有行的列表
>>> lines = f.readlines()
>>> lines
['1\t2\t3\t4\t5\n', '6\t7\t8\t9\t10\n', '11\t12\t13\t14\t15\n', '16\t17\t18\t19\t20']
#关闭文件
>>> f.close()

This reads all the lines, but it is not the result we want, because the Tab separator between elements has not been removed, and the carriage return and line feed characters at the end of the line have not been removed.

Here are three steps:

Read each line first

#定义一个空列表lineArr 
lineArr = []
#打开源文件test.txt
f = open('test.txt')
#readlines() 读取整个文件,自动将文件内容分析成一个包含所有行的列表lines
lines = f.readlines()
#遍历lines的每一行line
for line in lines:
    #将当前行的内容添加到列表lineArr 
    lineArr.append(line)
    #打印出当前的lineArr列表
    print("lineArr:", lineArr)
#打印出最终的lineArr列表
print("最终的lineArr:",lineArr)
#关闭文件
f.close()

运行结果:
lineArr: ['1\t2\t3\t4\t5\n']
lineArr: ['1\t2\t3\t4\t5\n', '6\t7\t8\t9\t10\n']
lineArr: ['1\t2\t3\t4\t5\n', '6\t7\t8\t9\t10\n', '11\t12\t13\t14\t15\n']
lineArr: ['1\t2\t3\t4\t5\n', '6\t7\t8\t9\t10\n', '11\t12\t13\t14\t15\n', '16\t17\t18\t19\t20']
最终的lineArr: ['1\t2\t3\t4\t5\n', '6\t7\t8\t9\t10\n', '11\t12\t13\t14\t15\n', '16\t17\t18\t19\t20']

Then remove each line first, and then use the Tab character as the separator between the elements
 

#定义一个空列表lineArr 
lineArr = []
#打开源文件test.txt
f = open('test.txt')
#readlines() 读取整个文件,自动将文件内容分析成一个包含所有行的列表lines
lines = f.readlines()
#遍历lines的每一行line
for line in lines:
    #将每一行先去掉回车,再以Tab符作为元素之间的分隔符号
    linenew = line.strip().split('\t')
    #将当前行的内容添加到列表lineArr 
    lineArr.append(linenew)
    #打印出当前的lineArr列表
    print("lineArr:", lineArr)
#打印出最终的lineArr列表
print("最终的lineArr:",lineArr)
#关闭文件
f.close()

运行结果:
lineArr: [['1', '2', '3', '4', '5']]
lineArr: [['1', '2', '3', '4', '5'], ['6', '7', '8', '9', '10']]
lineArr: [['1', '2', '3', '4', '5'], ['6', '7', '8', '9', '10'], ['11', '12', '13', '14', '15']]
lineArr: [['1', '2', '3', '4', '5'], ['6', '7', '8', '9', '10'], ['11', '12', '13', '14', '15'], ['16', '17', '18', '19', '20']]
最终的lineArr: [['1', '2', '3', '4', '5'], ['6', '7', '8', '9', '10'], ['11', '12', '13', '14', '15'], ['16', '17', '18', '19', '20']]

Finally, transform each character number into an integer number

#定义一个空列表lineArr 
lineArr = []
#打开源文件test.txt
f = open('test.txt')
#readlines() 读取整个文件,自动将文件内容分析成一个包含所有行的列表lines
lines = f.readlines()
#遍历lines的每一行line
for line in lines:
    #将每一行先去掉回车,再以Tab符作为元素之间的分隔符号
    linenew = line.strip().split('\t')
    #len(linenew)是linenew列表长度,也就是linenew列表的元素个数
    #for in in range(m)表示i从0增加到m-1
    for i in range(len(linenew)):
    	#从linenew[0]到linenew[m-1]的每个元素都转为整型数字
    	linenew[i] = int(linenew[i])
    #将当前行的内容添加到列表lineArr 
    lineArr.append(linenew)
    #打印出当前的lineArr列表
    print("lineArr:", lineArr)
#打印出最终的lineArr列表
print("最终的lineArr:",lineArr)
#关闭文件
f.close()

运行结果:
lineArr: [[1, 2, 3, 4, 5]]
lineArr: [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]]
lineArr: [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15]]
lineArr: [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15], [16, 17, 18, 19, 20]]
最终的lineArr: [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15], [16, 17, 18, 19, 20]]

This completes the data processing of the original file. I will talk about loops and traversals later.

Since IOError (unexpected circumstances such as file not found) may occur during file reading and writing, once an error occurs, subsequent f.close () will not be called. Therefore, in order to ensure that the file can be closed correctly regardless of whether there is an error, we can use try ... finally to achieve:

try:
    f = open('test.txt', 'r')
    print(f.read())
finally:
    if f:
        f.close()

But it is too cumbersome to do this every time, so Python introduced the with statement to automatically call the close () method for us automatically:

with open('test.txt', 'r') as f:
    print(f.read())

This way you don't have to worry about the file not being closed.

to sum up:

Python's single-line input uses the input () function, but regardless of the input, Python considers it to be a string type.

There are three steps to reading and writing Python files: open, read, and close. There are three ways to read: read, readline, readlines. You can use the with statement to omit the step of closing the file.

 

My CSDN blog column: https://blog.csdn.net/yty_7

Github address: https://github.com/yot777/

If you think this chapter is helpful to you, welcome to follow, comment and like! Github welcomes your Follow and Star!

 

Published 55 original articles · won praise 16 · views 6111

Guess you like

Origin blog.csdn.net/yty_7/article/details/104335684