制造数据: 每行101个随机数字。
import randomdef get_file(): """ 获得一个文件,如果没有,则;创建并返回,如果有,则直接返回。 :return: file """ file = open('data.txt', 'a', encoding='utf-8') return filedef append_data(file, data_num): """ 获取数据 :param file: :return: """ for i in range(data_num): tmp_data = random.randrange(0, data_num) tmp_data = str(tmp_data) + ' ' file.write(tmp_data) if i != 0 and i % 100 == 0: file.write('\n') print(i)if __name__ == '__main__': file = get_file() data_num = 100000000000000000000 append_data(file, data_num) file.close()
获取文件中的单个行:
if __name__ == '__main__': file = open('data.txt', 'r', encoding='utf-8') try: while True: line = file.readline() if line: words = line.split(' ') print("line=", len(words)) else: break finally: file.close()