0
点赞
收藏
分享

微信扫一扫

CentOS 7 部署 Nacos-2.3.0 (单机版)

深夜瞎琢磨 2023-12-19 阅读 39

目录

​编辑

10.1.6 包含一百万位的大型文件

pi_string.py

10.1.7 圆周率值中包含你的生日吗

10.2 写入文件

10.2.1 写入空文件

write_message.py

programming.txt

10.2.2 写入多行

10.2.3 附加到文件

write_message.py

programming.txt

10.3 异常

10.3.1 处理 ZeroDivisionError 异常

division.py

10.3.2 使用 try-except 代码块

10.3.3 使用异常避免崩溃

division.py

往期快速传送门👆(在文章最后):

感谢大家的支持!欢迎订阅收藏!专栏将持续更新!


10.1.6 包含一百万位的大型文件

filename = 'pi_million_digits.txt'
with open(filename) as file_object:
 lines = file_object.readlines()
pi_string = ''
for line in lines:
 pi_string += line.strip()
print(pi_string[:52] + "...")
print(len(pi_string)) 
3.14159265358979323846264338327950288419716939937510...
1000002

注意 要运行这个程序(以及后面的众多示例),你需要从https://www.nostarch.com/pythoncrashcourse/下载相关的资源。


10.1.7 圆周率值中包含你的生日吗

filename = 'pi_million_digits.txt'
with open(filename) as file_object:
 lines = file_object.readlines()
pi_string = ''
for line in lines:
 pi_string += line.rstrip()
1 birthday = input("Enter your birthday, in the form mmddyy: ")1
2 if birthday in pi_string:
 print("Your birthday appears in the first million digits of pi!")
else:
 print("Your birthday does not appear in the first million digits of pi.")
Enter your birthdate, in the form mmddyy: 120372
Your birthday appears in the first million digits of pi! 

10.2 写入文件

10.2.1 写入空文件

filename = 'programming.txt'
1 with open(filename, 'w') as file_object:
2 file_object.write("I love programming.")
I love programming. 

注意 Python只能将字符串写入文本文件。要将数值数据存储到文本文件中,必须先使用函数 str()将其转换为字符串格式。


10.2.2 写入多行

I love programming.I love creating new games. 
filename = 'programming.txt'
with open(filename, 'w') as file_object:
 file_object.write("I love programming.\n")
 file_object.write("I love creating new games.\n")
I love programming.
I love creating new games. 

10.2.3 附加到文件

filename = 'programming.txt'
1 with open(filename, 'a') as file_object:
2 file_object.write("I also love finding meaning in large datasets.\n")
file_object.write("I love creating apps that can run in a browser.\n")
I love programming.
I love creating new games.
I also love finding meaning in large datasets.
I love creating apps that can run in a browser.

10.3 异常

10.3.1 处理 ZeroDivisionError 异常

print(5/0)
Traceback (most recent call last):
 File "division.py", line 1, in <module>
 print(5/0)
1 ZeroDivisionError: division by zero

10.3.2 使用 try-except 代码块

try:
 print(5/0)
except ZeroDivisionError:
 print("You can't divide by zero!") 
You can't divide by zero!

10.3.3 使用异常避免崩溃

print("Give me two numbers, and I'll divide them.")
print("Enter 'q' to quit.")
while True:
1 first_number = input("\nFirst number: ")
if first_number == 'q':
 break
2 second_number = input("Second number: ")
if second_number == 'q':
 break
3 answer = int(first_number) / int(second_number)
 print(answer) 
Give me two numbers, and I'll divide them.
Enter 'q' to quit.
First number: 5
Second number: 0
Traceback (most recent call last):
 File "division.py", line 9, in <module>
 answer = int(first_number) / int(second_number)
ZeroDivisionError: division by zero 

关于“Python”的核心知识点整理大全12-CSDN博客

往期快速传送门👆(在文章最后):

感谢大家的支持!欢迎订阅收藏!专栏将持续更新!

举报

相关推荐

0 条评论