一、文件的打开和创建
>>> f=open('/tmp/test.txt')
>>> f.read()
'hello python!hello world!'
>>> f
二、文件的读取
步骤打开
--
读取
--
关闭
>>> f=open('/tmp/test.txt')
>>> f.read()
'hello python!hello world!'
>>> f.close()
读
取
数
据
是
后
期
数
据
处
理
的
必
要
步
骤
。
.txt
是
广
泛
使
用
的
数
据
文
件
格
式
。
一
些
.csv, .xlsx
等文件可以转换为
.txt
文件进行读取。我常使用的是
Python
自带的
I/O
接
口,
将数据读取进来存放在
list
中,
然后再用
numpy
科学计算包将
list
的数据转换为
array
格式,从而可以像
MATLAB
一样进行科学计算。
下面是一段常用的读取
txt
文件代码,可以用在大多数的
txt
文件读取中
filename='array_reflection_2D_TM_vertical_normE_center.txt' # txt
文件和当前
脚本在同一目录下,所以不用写具体路径
pos=[]
Efield=[]
with open(filename, 'r') as file_to_read:
while True:
lines=file_to_read.readline() #
整行读取数据
if not lines:
python读文本文件的过程是怎样的_读写文本文件的步骤_Python读写txt文本文件的操作方法全解析...