其他分享
首页 > 其他分享> > 第4章 4.3 读取文件

第4章 4.3 读取文件

作者:互联网

一、读取文件,并通过筛选后打印出来:

(.venv) (base) metal@metal-Lenovo-Product:~/project/PAutomationCookbook/ch04$ cat zen_of_python.txt
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
(.venv) (base) metal@metal-Lenovo-Product:~/project/PAutomationCookbook/ch04$
(.venv) (base) metal@metal-Lenovo-Product:~/project/PAutomationCookbook/ch04$
(.venv) (base) metal@metal-Lenovo-Product:~/project/PAutomationCookbook/ch04$ python
Python 3.7.6 (default, Jan 8 2020, 19:59:22)
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> with open('zen_of_python.txt') as file:
... for line in file:
... print(line)
...
The Zen of Python, by Tim Peters

 

Beautiful is better than ugly.

Explicit is better than implicit.

Simple is better than complex.

Complex is better than complicated.

Flat is better than nested.

Sparse is better than dense.

Readability counts.

Special cases aren't special enough to break the rules.

Although practicality beats purity.

Errors should never pass silently.

Unless explicitly silenced.

In the face of ambiguity, refuse the temptation to guess.

There should be one-- and preferably only one --obvious way to do it.

Although that way may not be obvious at first unless you're Dutch.

Now is better than never.

Although never is often better than *right* now.

If the implementation is hard to explain, it's a bad idea.

If the implementation is easy to explain, it may be a good idea.

Namespaces are one honking great idea -- let's do more of those!

>>>

去掉字符串前后的空格才看起来比较舒服:

>>> with open('zen_of_python.txt', 'r') as file:
... for line in file:
... print(line.strip())
...
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

 

 

 


>>> with open('zen_of_python.txt', 'r') as file:
... for line in file:
... if 'should' in line.lower():
... print(line)
...
Errors should never pass silently.

There should be one-- and preferably only one --obvious way to do it.

>>>
>>> with open('zen_of_python.txt', 'rt') as file:
... for line in file:
... if 'better' in line.lower():
... print(line)
... break

...
Beautiful is better than ugly.

#'rt'指只读的文本模式打开。

 

 

 

 

with 上下文管理器是一种处理文件非常方便的方法,因为它将在使用后(离开代码块)关闭文件,即使出现异常也是这样。

另外,传统的方式打开文件如下:

>>> file = open('zen_of_python.txt')
>>> content = file.read()
>>> print(content)
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

>>> file.close()

 

标签:文件,读取,4.3,...,--,never,idea,better,than
来源: https://www.cnblogs.com/DreamToAwakening/p/16129506.html