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,将分列读取。