Python读取txt文件

F.read()和F.readlines():

复制代码
 1 #Python读取txt
 2 def Fread():
 3     print('-----read by .read()-----')
 4     with open('test.txt', encoding='utf-8') as file:
 5         content = file.read()
 6         print('content: ', content, 'type of content: ', type(content)) #输出原P.txt  type of content:  <class 'str'>
 7 
 8 def Freadlines():
 9     print('-----read by .readlines()-----')
10     with open('test.txt', encoding='utf-8') as file:
11         content = file.readlines()
12         print('content: ', content, 'type of content: ', type(content)) #输出列表,每一行为一项  type of content:  <class 'list'>
13 
14 if __name__ == '__main__':
15     Freadlines()
复制代码

 

numpy:

fname要读取的文件、文件名、或生成器。

dtype数据类型,默认float。

comments注释。

delimiter分隔符,默认是空格。

skiprows跳过前几行读取,默认是0,必须是int整型。

usecols要读取哪些列,0是第一列。例如,usecols = (1,4,5)将提取第2,第5和第6列。默认读取所有列。

unpack如果为True,将分列读取。

 

posted @   Xxiaoyu  阅读(2534)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示