我的python学习笔记、从文件中读取数据
来源:互联网 发布:过滤器返回json 编辑:程序博客网 时间:2024/05/23 17:46
1、读取整个文件
1)使用函数open()打开文件,函数open()接收一个参数:要打开文件的名称。python在当前执行的文件所在的目录查找指定文件。
函数open()返回一个表示文件的对象。使用方法如下:
with open("data.txt") as file_object
2)python将这个对象存储在我们将在后面使用的变量中。
3)关键字with在不再需要访问文件后将其关闭。
也可以使用open和close来打开和关闭文件,但这样做时,如果程序出现bug,导致close语句未执行,文件将不会关闭,这看起来微不足道,但是未妥善的关闭文件可能导致数据丢失或受损。但通过with结构可让python去确定,你只管打开文件,并在需要时使用它,python自会在合适的时候将其自动关闭。
4)使用read函数读取文件的全部内容,使用方法如下:
contents=file_object.read()
但是read到达文件末尾时返回一个空字符串,而将这个空字符串显示出来就是一个空行。要删除末尾的空行,可在print语句中使用rstrip()--python使用rstrip()删除字符串末尾的空白。
实例:
#data.txt
3.1415926535
8979323846
2643383279
#file_reader.py
with open("data.txt") as file_object:
contents=file_object.read()
print(contents)
输出为:
D:\www>python file_reader.py
3.1415926535
8979323846
2643383279
2、文件路径
有时需要打开不在程序文件所属目录中的文件,这时可采用相对路径或绝对路径来打开文件。
在windows中,文件路径中使用反斜杠(\)而不是斜杠(/)
由于反斜杠在python中被视为转义标记,为在windows中确保万无一失,应以原始字符串的方式指定
3、逐行读取
可通过对文件对象执行循环来遍历文件中的每一行
#file_reader.py
with open("D:\data.txt") as file_object:
for line in file_object:
print(line)
输出为:
D:\www>python file_reader.py
#data.txt
3.1415926535
8979323846
2643383279
我们打印每一行时,发现空白行更多了,因为在这个文件中,每行的末尾都有一个看不见的换行符,而print语句也会加上一个换行符,因此每行的末尾都有两个换行符:一个来自文件,另一个来自print语句。要删除这些多余的空白行,可在每条print语句后面使用rstrip():
#file_reader.py
with open("D:\data.txt") as file_object:
for line in file_object:
print(line.rstrip())
输出为:
D:\www>python file_reader.py
#data.txt
3.1415926535
8979323846
2643383279
4、创建一个包含文件各行内容的列表
使用关键字with时,open返回的文件对象,只在with代码块内可用。如果要在with代码块外访问文件的内容,可在with代码块内将文件的各行存储在一个列表中,并在with代码块外使用该列表。
方法readlines()从文件中读取每一行,并将其存储在一个列表lines中。由于列表lines的每个元素都对应于文件中的一行,因此输出与文件内容完全一致。
#file_reader.py
with open("D:\data.txt") as file_object:
lines=file_object.readlines()
for line in lines:
print(line.rstrip())
输出为:
D:\www>python file_reader.py
#data.txt
3.1415926535
8979323846
2643383279
5、使用文件的内容
#file_reader.py
with open("D:\data.txt") as file_object:
lines=file_object.readlines()
pi_string=''
for line in lines:
pi_string+=line.rstrip()
print(pi_string)
print(len(pi_string))
输出为:
D:\www>python file_reader.py
#data.txt3.1415926535 8979323846 2643383279
45
在变量pi_string存储的字符串中,包含原来位于每行左边的空格,为删除这些空格,可使用strip()而不是rstrip():
#file_reader.py
with open("D:\data.txt") as file_object:
lines=file_object.readlines()
pi_string=''
for line in lines:
pi_string+=line.strip()
print(pi_string)
print(len(pi_string))
输出为:
D:\www>python file_reader.py
#data.txt3.141592653589793238462643383279
41
注意:读取文本文件时,python将其中的所有文本都解读为字符串。如果你读取的数字,并要将其作为数值使用,就必须使用int()将其转换为整数,或使用函数float()将
其转换为浮点数。
6、控制输出的数量
#file_reader.py
with open("D:\data.txt") as file_object:
lines=file_object.readlines()
pi_string=''
for line in lines:
pi_string+=line.strip()
print(pi_string[:20]+'...')
#只显示前20位
输出为:
D:\www>python file_reader.py
#data.txt3.141592653...
7、实例——圆周率中包含你的生日吗
#file_reader.py
with open("D:\data.txt") as file_object:
lines=file_object.readlines()
pi_string=''
for line in lines:
pi_string+=line.strip()
birthday=input("Enter your birthday,in the form mmddyy: ")
if birthday in pi_string:
print("Your birthday appears in the pi!")
else:
print("Your birthday does not appear in the pi!")
输出为:
D:\www>python file_reader.py
Enter your birthday,in the form mmddyy: 960312
Your birthday does not appear in the pi!
- 我的python学习笔记、从文件中读取数据
- C#学习笔记:从文件中读取数据
- Python--从文件中读取数据
- Python从txt文件中逐行读取数据
- Python从txt文件中逐行读取数据
- python matplotlib从文件中读取数据绘制散点图
- python matplotlib从文件中读取数据绘制折线图
- 从文件中读取数据
- 从文件中读取数据
- 从文件中读取数据
- 我的python学习笔记、存储数据
- 我的python学习笔记、写入文件
- python 中os.path.append(),设定从kafka读取文件的时间 如:从kafka中读取半个小时的数据
- 从Java的jar文件中读取数据
- matlab读入从文件中读取大量的数据
- Java从文件中读取字节数据的固定套路
- springMVC从上传的Excel文件中读取数据
- springMVC从上传的Excel文件中读取数据
- 联网,网络判断的实现方式
- atom 不同窗口打开同一文件
- .net/C# 进程和线程
- 出现 warning: Clock skew detected. Your build may be incomplete.这样的警告
- 为何网站需要静态化
- 我的python学习笔记、从文件中读取数据
- 进行网站开发是如何选择域名
- Qt 设置全局字体
- textarea 输入框限制字数 时间:2014-07-16 20:56:54 阅读:1948 评论:0 收藏:0 [点我收藏+] 标签:style ja
- MySQL体系结构
- 微信小程序之封装请求函数
- img{display:block}
- 设置初始工作文件夹一般方法---在matlab2017失效的解决方法
- 编写可靠 shell 脚本的 8 个建议