python read方法

2024-10-12 06:08:58

1、首先,我们先看一下代码的结构,就是一个文件夹,然后里面一些python运行文件和存储数据的txt文件。

python read方法

3、我这里复制一下开始那张图片的内容:# -*- coding: UTF-8 -*-# 使用with open("文件名) as 参数 的形式打开文围泠惶底件 这种形式省略了对于文件的关闭close# 1.读取整个文件with open("demo.txt",encoding='UTF-8') as f: content=f.read() print(content)print("===================")# 2.逐行读取 使用for循环逐行读取,注意每行有一个回车符号\nwith open("demo.txt",encoding='UTF-8') as f: for line in f: print(line.rstrip())print("===================")#3.使用readlines()来读取 方法 readlines() 从文件中读取每一行,并将其存储在一个列表中with open("demo.txt",encoding='UTF-8') as f: lines=f.readlines()print(lines)然后展示游戏执行结果,然详细的备注也里面

python read方法

5、解决这个关于中文出错的问题,说是还有一种解决方法,但是我使用另一种方法的时候,虽然没有出错,但是显示的是乱码

python read方法

6、说了python中读取文件内容的read方法,这里继续说一下python写的方法write

python read方法
猜你喜欢