Python读取txt文件,有两种方式: (1)逐行读取 data=open("data.txt") line=data.readline() while line: print line line=data.readline() (2)一次全部读入内存 data=open("data.txt") for line in data.readlines(): print line
python操作txt文件中数据教程[1]-使用python读写txt文件 觉得有用的话,欢迎一起讨论相互学习~Follow Me 原始txt文件 程序实现后结果 程序实现 filename = './test/test.txt' contents = [] DNA_sequence = [] # 打开文本并将所有内容存入contents中 with open(filename, 'r') as f: for line in f.readlines(): contents.append(line
读数据代码: with open(path,'r') as f: for line in f: line = line.strip() 报错: UnicodeDecodeError: 'gbk' codec can't decode byte 0xac in position 451428: illegal multibyte sequence 尝试修改代码为: with open(path,encoding="UTF-8") 又报其他错误: UnicodeDecodeError: '